I have a very strange error on my code, which I run in VS 2015 and eclipse:
int main(int argc, const char**argv) {
FILE *input = stdin;
FILE *output = stdout;
if(!argumentsValid(argv, argc)){
mtmPrintErrorMessage(stderr, MTM_INVALID_COMMAND_LINE_PARAMETERS);
return 1;
}
if(!changeIO(&input, &output, argv, argc)){
mtmPrintErrorMessage(stderr, MTM_CANNOT_OPEN_FILE);
return 1;
}
char buffer[MAX_LEN + 1];
Yad3Service yad3Service = yad3ServiceCreate();
if(!yad3Service){
mtmPrintErrorMessage(stderr, MTM_OUT_OF_MEMORY);
deallocateAndClose(input, output, &yad3Service);
}
int j = 0;
while (fgets(buffer, MAX_LEN, input) != NULL) {
j++;
printf("%d\n", j);
char command_p1[10];
strcpy(command_p1, strtok(buffer, " "));
if (!strcmp(command_p1, "\n") || command_p1[0] == '#') continue;
char command_p2[30];
strcpy(command_p2 + 1, strtok(NULL, " "));
command_p2[0] = ' ';
char command[40];
strcpy(command, strcat(command_p1, command_p2));
char* command_arguments[10];
int i = 0;
while((command_arguments[i++] = strtok(NULL, " ")));
Yad3ServiceResult res = command_parser(&yad3Service, command,
command_arguments, output);
if(res != YAD3_SUCCESS){
if(res == YAD3_OUT_OF_MEMORY){
deallocateAndClose(input, output, &yad3Service);
mtmPrintErrorMessage(stderr, ((MtmErrorCode)((int)res)));
return 1;
}
mtmPrintErrorMessage(stderr, ((MtmErrorCode)((int)res)));
}
}
deallocateAndClose(input, output, &yad3Service);
return 0;
}
The problem which visual studio gives is in the end of the program, after the return command:
Run-Time Check Failure #2 - Stack around the variable 'command_p1' was corrupted.
and eclipse is doing something even stranger, at 34 iteration in the while, the inner fields of Yad3Service (which is a struct with allocated memory in it) cleared and the inner addresses doesn't seemed to be there (in debugging), they just disappear, and the next time I access to the inner fields of the pointer of the struct, I get Segmentation Fault.
Maybe its about the memory which is corrupted some how during the strcpy or the strtok, I dont understand what happens. Somebody ???
The strange disappearing happens after doing the line:
strcpy(command, strcat(command_p1, command_p2));