Skip to content

Instantly share code, notes, and snippets.

@sohay666
Last active October 27, 2023 07:35
Show Gist options
  • Save sohay666/762ccfa39d9d93785242cd6b65e1a7ff to your computer and use it in GitHub Desktop.
Save sohay666/762ccfa39d9d93785242cd6b65e1a7ff to your computer and use it in GitHub Desktop.
Example integer overflow
#include <stdio.h>
//http://phrack.org/issues/60/10.html
//https://www.geeksforgeeks.org/maximum-value-of-unsigned-short-int-in-c/
//gcc -o integer_overflow integer_overflow.c
int main(){
unsigned short s;
int i,j;
printf("Please input [1-666]:");
scanf("%d",&i);
//triger the integer overflow (When this value is transferred into the short integer s,
//it is truncated if the value is too great to fit into s (i.e. if the value is greater than 65535)
s = i;
j = 666;
if(s >= j){
printf("You can't input more than %d\n",j);
return -1;
}
if(s == (j-1)) { //66201
printf("congratulations win :)\n");
}else{
printf("Sorry %d is not your lucky number\n", s);
}
return 0;
}
/*
longcat@kucingliar$gcc -o integer_overflow integer_overflow.c
longcat@kucingliar$./integer_overflow
Please input [1-666]:-1
You can't input more than 666
longcat@kucingliar$./integer_overflow
Please input [1-666]:100
Sorry 100 is not your lucky number
longcat@kucingliar$./integer_overflow
Please input [1-666]:6777
You can't input more than 666
longcat@kucingliar$./integer_overflow
Please input [1-666]:66201
congratulations win :)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment