The program takes a pointer to a char array and an int. The char array consists of two numbers, separated by a space.
The use of the function is to read the values of the char array as integers and replace them with the multiplied value of the input:
void read_and_mul(char *arr, int scale) {
int num_arr[2]; // saving values in a int[]
char *ch = strtok(arr, " ");
num_arr[0] = scale * (atoi(ch));
ch = strtok(NULL, " ");
num_arr[1] = scale * (atoi(ch));
memset(arr, 0, sizeof(arr)); // deleting the previous value of the char[]
char one[sizeof(int)];
char two[sizeof(int)];
sprintf(one, "%d", num_arr[0]); // saving the altered numbers as chars
sprintf(two, "%d", num_arr[1]);
strcat(arr, one); // writing the multiplied values to the string
strcat(arr, " ");
strcat(arr, two);
}
However if I use it like this, it works as intended but causes a stack-smashing:
int main(int argc, char *argv[]) {
char str[] = "1 2";
read_and_mul((char *)&str, 10);
printf("string after call: %s\n", str);
return 0;
}
The terminal message in CLion is:
*** stack smashing detected ***: terminated
string after call: 10 20
Is this a potential error or an IDE warning and what is causing it?
The function has to build the string
"10 20"that contains6characters including the terminating null character'\0'.But you are trying to store this string in an array that has only
4charactersdue to these statements
As a result the function already invokes undefined behavior.
Another problem is in this call of
memset:The variable
arrwithin the function has the pointer typechar *. Ifsizeof( char * )is equal to8then again there is an attempt to write to memory outside the array.And the function should not depend on magic numbers like
2used in this declarationYou should always try to write more general functions.
To resolve the problem you should within the function allocate dynamically a new character array where the result string will be stored and return a pointer to the array from the function.
Also, pay attention to that it will be more clear and correct to write
instead of
Here is a demonstration program that shows a possible approach to solve the task.
The program output is
As in general multiplication of two integers can result in overflow then to avoid such a situation you may change these statements
to the following