I am having problems passing an array of strings to a function by reference in C.
Context: I am trying to make a very simple shell with a few commands. When the commands are typed in each argument of the command is saved in a slot of an array of strings. The function used to parse the command is called lineParsing (I do not have the source for this – I only have the .o and .h)
The function in question has the following header:
void lineParsing (char **, int, char **, char **, char **, int *);
void lineParsing (char **item, int nargs, char **inputRedir, char **outputRedir, char **errorRedir, int *background);
The description of the function is as follows:
Specifies if the array of arguments (first argument), contains a redirection or background execution.
Inputs:
1-the array of arguments
2-number of arguments
by reference:
3-An array of strings in which to place the names of the files for input redirection
4-An array of strings in which to place the names of the files for output redirection
5-An array of strings in which to place the names of the files for error redirection
6-Saves 1 in background if & is used, if not 0
I am calling this function in a temporary main program as follows:
int main (int argc, char *argv[]){
char **parrayArgumentos=NULL;
int i,numargs,background;
char *inputRedir[4]={"","","",""};
char *outputRedir[4]={"","","",""};
char *errorRedir[4]={"","","",""};
parrayArgumentos = lineInput (&numargs); //Asks for command via standard input.
printf ("You have typed in: %d arguments \n",numargs); //displays number of arguments
i=0;
while(i<=numargs-1){
printf ("%s \n",parrayArgumentos[i]);
i++;
}
lineParsing(parrayArgumentos,numargs,inputRedir,outputRedir,errorRedir,&background);
//This call is problematic
printf ("The command you have introduced has:\n%c for input redirection\n%c for output Redirection\n%s For error Ridirection\n%d background\n",inputRedir[0],outputRedir[0],errorRedir[0],background);
freeLineInput(parrayArgumentos);
return 0;
}
This compiles with no errors or warnings via a makefile I have made:
mishell: mishell.o parser64.o
gcc mishell.o parser64.o -o mishell
mishell.o: mishell.c
clean:
rm -f mishell.o mishell
When I execute the binary I get the following error at the point in which the call to lineParsing occurs:
[xxxx@xxxx src]$ ./mishell
ls -la > listoffiles
You have typed in: 4 arguments
ls
-la
>
listoffiles
*** glibc detected *** ./mishell: free(): invalid pointer: 0x0000000000401078 ***
======= Backtrace: =========
/lib/libc.so.6(+0x784a6)[0x7f8a5f76b4a6]
./mishell[0x400d99]
./mishell[0x4007b6]
/lib/libc.so.6(__libc_start_main+0xf5)[0x7f8a5f714725]
./mishell[0x4005e9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:04 7865560 /home/xxxxx/Desktop/xxxx/xxxx/xxxxx/src/mishell
00601000-00602000 rw-p 00001000 08:04 7865560 /home/xxxxx/Desktop/xxxxx/xxxxx/xxxxx/src/mishell
00e25000-00e46000 rw-p 00000000 00:00 0 [heap]
7f8a5f4de000-7f8a5f4f3000 r-xp 00000000 08:03 1185891 /usr/lib/libgcc_s.so.1
7f8a5f4f3000-7f8a5f6f2000 ---p 00015000 08:03 1185891 /usr/lib/libgcc_s.so.1
7f8a5f6f2000-7f8a5f6f3000 rw-p 00014000 08:03 1185891 /usr/lib/libgcc_s.so.1
7f8a5f6f3000-7f8a5f88e000 r-xp 00000000 08:03 2490393 /lib/libc-2.16.so
7f8a5f88e000-7f8a5fa8d000 ---p 0019b000 08:03 2490393 /lib/libc-2.16.so
7f8a5fa8d000-7f8a5fa91000 r--p 0019a000 08:03 2490393 /lib/libc-2.16.so
7f8a5fa91000-7f8a5fa93000 rw-p 0019e000 08:03 2490393 /lib/libc-2.16.so
7f8a5fa93000-7f8a5fa97000 rw-p 00000000 00:00 0
7f8a5fa97000-7f8a5fab8000 r-xp 00000000 08:03 2490410 /lib/ld-2.16.so
7f8a5fc8a000-7f8a5fc8d000 rw-p 00000000 00:00 0
7f8a5fcb4000-7f8a5fcb8000 rw-p 00000000 00:00 0
7f8a5fcb8000-7f8a5fcb9000 r--p 00021000 08:03 2490410 /lib/ld-2.16.so
7f8a5fcb9000-7f8a5fcba000 rw-p 00022000 08:03 2490410 /lib/ld-2.16.so
7f8a5fcba000-7f8a5fcbb000 rw-p 00000000 00:00 0
7fff8533d000-7fff8535e000 rw-p 00000000 00:00 0 [stack]
7fff853ff000-7fff85400000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted
I am using gcc 4.7.1 under ArchLinux 3.4.4-2-ARCH x86_64
Does anyone know why this is happening?
Thank you very much for all help in advance
A possible cause would be an attempt to free any of the elements in
inputRedir,outputRedirorerrorRedirif they are unchanged. As they are initialised to a string literal (empty string) passing this tofree()is illegal:A speculative fix would be to initialise the elements to NULL pointers (it is safe to pass a NULL pointer to
free()) for these three arrays:Note that the format specifiers in the
printf()statement are incorrect asinputRedir[0](andoutputRedir[0]) is achar*but the format specifier provided is%c, which is for typechar: use%sforchar*.