Operator == doesn't work in C - c

I'm getting a weird error while writing my program in C.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include "defs.h"
int main (int argc, char* argv[])
{
int boardSize = atoi(argv[2]);
int generations = atoi(argv[4]);
int gamesort = atoi(argv[1]);
printf("2 is %d 1 is %d 4 is %d name of file is %s \n",boardSize,gamesort,generations,argv[3]);
if (1==1)
{
printf("yes");
ZeroPlayersGame(boardSize, generations,argv[3]);
}
else//(gamesort==2)
{
TwoPlayersGame(boardSize, generations,argv[3]);
}
return 0;
}
And here is the error im getting from the Terminal:
ise#ubuntu:~/Desktop$ make
gcc -c main.c defs.c gameIO.c zeroPlayer.c twoPlayer.c
gcc gameIO.o defs.o zeroPlayer.o main.o twoPlayer.o -o prog
ise#ubuntu:~/Desktop$ ./prog 1 2 "l.txt" 3
2 is 2 1 is 1 4 is 3 name of file is l.txt
Segmentation fault (core dumped)
Very strange, as you can see my program dosen't enter my first "if",
but you can see it prints the line before the if statment.
thanks for any help!

I think your problem is not related to your question title "Operator == doesn't work in C". As seen in your output, printf works well and there may be possibly a problem in functions "ZeroPlayersGame" and "TwoPlayersGame". If you gave us more information about these two functions, solving the problem would be easier.
Also, using "1==1" in logical statement is so weird. Why don't you eliminate it and write your statement just after the printf statement. Because "1==1" is always true.

Related

AFL-fuzz not finding any crashes

I am trying AFL for the first time, and for that reason i found a very simple vulnerable C code that i could use to test AFL.
The C code in questions is
#include <stdio.h>
#include <string.h>
int main(int argc, char * argv[]){
char name[10];
if ( argc > 1 ){
strcpy(name, argv[1]);
printf("HELLO %s\n", name);
}
return 0;
}
I compile that code by running afl-gcc test.c -o test and i tested it just to make sure it crashes when it was suppose to (running ./test $(python3 -c "print('A'*26)") will give a segmentation fault as expected)
The problem here is, i created a testcase echo -en "test\x00" > input/testcase and run AFL afl-fuzz -i afl_in -o afl_out -- ./test but after a day it still hasn't found any crashes.
I also tried to create a test case that would force it crash python3 -c "print('A'*26)" > input/testcase but it still runs and does not find anything.
This was suppose to be the easiest example so i could get to know AFL a bit better but it is proving to be a challege. Can anyone help?
Just as Nick ODell post it in the comments
Seems like AFL expects the program under test to read from STDIN rather than an argument. github.com/google/AFL#6-fuzzing-binaries
Following that URL shows an experimental module that allows for AFL to read from an argument, and for that to work i just had to add 2 lines to my existing code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "argv-fuzz-inl.h" // <-- Argv fuzz module
int main(int argc, char * argv[]){
AFL_INIT_ARGV(); // <-- needed near the very beginning of main().
char name[10];
if ( argc > 1 ){
strcpy(name, argv[1]);
printf("HELLO %s\n", name);
}
return 0;
}
After that i just compiled it again and everything worked as expected.

Read an integer from stdin (in C)

I want to write a C program that takes as input an integer, and outputs its square. Here is what I have tried.
However,
./a.out < 3 outputs 32768, and
./a.out < 4 also outputs 32768.
Where am I wrong? Thanks.
#include <stdio.h>
int main(){
int myInt;
scanf("%d", &myInt);
printf("%d\n",myInt*myInt);
}
It looks like what you're trying to do is
echo 4 | ./a.out
the syntax for < is
program < input_file
whereas | is
command_with_output | program
./a.out < 4
This tries to read the file named 4 and use it's content as input to a.out You can do it whichever way, but understand the < operator isn't for inputting the character you type quite literally.
One way to do this would be:
echo "4" > 4
./a.out < 4
I just run your program and its works great.
If you want the program receive an integer input you should use argc , argv as folowed and not use scanf.
*The code for argc argv: *
#include <stdio.h>
#include <stdlib.h>
int main(int argc , char** argv)
{
int myInt;
myInt = atoi(argv[1]);
printf("%d\n",myInt*myInt);
}
atoi - convert char* to integer.
If you want to run the program and then insert an integer, you did it right!
you can read about atoi
To run this program you should comile and run from terminal:
gcc a.c -o a
./a 3
and you will receive:
9
On the right hand side of "<", there should be a file containing the input.
try this thing:
$ echo "3" > foo
$ ./a.out < foo
Read this for more information (Specially section 5.1.2.2):
http://www.tldp.org/LDP/intro-linux/html/chap_05.html

Count from 1 to 1000 without using loops corver edge

A follow up question to:
Print 1 to 1000 with out using loop
A question that was asked today and is a duplicate of an older question showing a code snippet of a recursion of the main function :
This code means to count to 10 without using any loops, it does it by recursing on the main functions pointer and when it reaches the needed amount of prints it changes the pointer to the exit function.
#include <stdio.h>
#include <stdlib.h>
void main(int j) {
printf("%d\n", j);
(&main + (&exit - &main)*(j/10))(j+1);
}
Note i changed the number of times the value will be printed,
I do understand that the subtraction of function pointers is UB but still I compiles and ran this function with:
./mainexit 547
And for some reason the values that are beeing printed are from 2 to 10.
when i start the program with ./mainexit 1 it will print until j reaches 10 and then the reference will be for exit but when I run the program with 547 from the beginning the first function in the recursion that is being called located at address 57*&exit so it should do really weird stuff but instead it prints normally. any ideas on how the function returns to the main?
You can do this nonsense like this:
/* Count from 1 to 10 using recursive main call */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int r;
if(argc>=11)
return EXIT_SUCCESS;
printf("argc=%d\n", argc);
r = main(argc+1, NULL);
return r;
}
And you can call
$./countmain
and get:
argc=1
argc=2
argc=3
argc=4
argc=5
argc=6
argc=7
argc=8
argc=9
argc=10
or call
$./countmain a b c d 4 4
and get:
argc=7
argc=8
argc=9
argc=10
But of course it would be much better if you just do something like:
/* Count from 1 to 10 using recursive function call */
#include <stdio.h>
#include <stdlib.h>
void reccount(int to);
int main(int argc, char *argv[])
{
int to;
if(argc==2)
to=atoi(argv[1]);
else
to=10;
reccount(to);
return EXIT_SUCCESS;
}
void reccount(int to)
{
if(to==0)
return;
reccount(to-1);
printf("to=%d\n", to);
return;
}
Now you type:
$ ./reccount.x
to=1
to=2
to=3
to=4
to=5
to=6
to=7
to=8
to=9
to=10
Or you can set the limit as:
$ ./reccount.x 2
to=1
to=2
Now, back to your question:
warning: ‘main’ takes only zero or two arguments [-Wmain]
warning: return type of ‘main’ is not ‘int’ [-Wmain]
If exit had a prototype like main, i.e., int exit(int x, char **y) you could do the trick by adding a conditional like this in the begin of the code:
if(argc==2 && argv!=NULL)
j=atoi(argv[1]);
else
j=argc;
and then calling them as:
(&main + (&exit - &main)*(j/10))(j+1, NULL);
Example:
#include <stdio.h>
#include <stdlib.h>
int myexit(int j, char **p)
{
exit(j);
}
int main(int argc, char **argv)
{
int j;
if(argc==2 && argv!=NULL)
j=atoi(argv[1]);
else
j=argc;
printf("%d\n", j);
(&main + (&myexit - &main)*(j/10))(j+1, NULL);
}
But as it stands, you just cant properly read argv from command line.
The parameter j is not used in the way you think.
When you launch the command, the first argument of main is the number of arg of the command line.
So launching with ./mainexit 547 just pass 2 as the first value of the first call to main. And the loop is built so that it calls exit when j reaches 10.
So the value of your arg is not useful, try ./mainexit hello it will give you the same result!

Glob function gives only one result in C

I have to program a shell in C and need to handle globing in it and I am only allowed to use the function glob. But when I try to use it, it only gives me one result back.
#include <glob.h>
#include <stdlib.h>
int main(int ac, char **av)
{
glob_t s;
unsigned long i = -1;
if (glob(av[1], 0, NULL, &s))
printf("ERROR !\n");
else
while (++i < s.gl_pathc)
printf("%s\n", s.gl_pathv[i]);
return (0);
}
I run this code in a folder where there is two C files : replace_glob.c and test.c
And when I run this code :
$ ./a.out *.c
replace_glob.c
$
I dont understand why and I would really appreciate your help
In the command line
./a.out *.c
the shell expands the glob pattern, so your program sees
{"./a.out", "replace_glob.c", "test.c", NULL}
as its argv. You need to quote the pattern for the program to see it:
./a.out '*.c'

GCC `scanf` segmentation fault

I was playing around with C and the scanf function and came across this weird error that I can't seem to figure out. Given the following code:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int a;
} sample;
void fn(sample *s) {
char command;
scanf("%[abc]", &command);
printf("Read: %c\n", command);
printf("In the sample function:, %i\n", s->a);
}
int main() {
sample *s = malloc(sizeof(sample));
s->a = 4;
printf("Before sample function: %i\n", s->a);
fn(s);
printf("After sample function: %i\n", s->a);
return 0;
}
It seems to seg fault. With the output:
$ ./sample
Before sample function: 4
a
Read: a
In the sample function:, 4
Segmentation fault (core dumped)
I used gdb and attached a watch to the struct, it seems that inside the scanf function, it seems to 'modify' the struct? Which is weird, because even after the scanf inside the sample function 'fn', it is able to print out the struct fields fine. However, once returning from the fn and jumping back into main, it seg faults when it tries to print out the same information?
Interestingly, if you change the scanf to scanf("%c\n", &command); (without the character set) it seems to work fine. For the record, the version of gcc I am using is 4.7.2, and I am compiling the code with: gcc -O0 -o sample sample.c.
My only thought is that perhaps character sets aren't supported by gcc? I'm not sure. Just wondering if anyone else could clear this up?
scanf("%[abc]", &command);
writes a string not a single character. The trailing null character of the string is being written in &command + 1 in your program.
You should pass to scanf something like:
command with command being:
char command[2];

Resources