3 ms·
Personally I don't agree with the post, starting from his analysis, of the first snippet of code: group->text = (char *) malloc(sizeof(char) * strlen(params-
by pietrofmaggi 16y ago
Personally I don't agree with the post, starting from his analysis, of the first snippet of code:
group->text = (char *) malloc(sizeof(char) * strlen(params->text));
if (group->text == NULL) {
res = MP_MEM;
}
strcpy(group->text, params->text);
I don't think that the sizeof(char) or the casting of the pointer are of any danger and increase readability (we are not coding to show how well we know the standard).
The poster is right about the missing space in the malloc call for the NULL char, but I see a bigger threat calling a malloc with the size coming from a strlen: it can be really dangerous if somehow you miss a NULL terminator.
And again, same complain about the usage of strcpy. The standard gives us strncpy that put a limiting size in the copy operation.
_Personally I think that strcpy has to be avoided as a plague being a source of buffer overruns error as no other call in the C library._
- lukatmyshu 16y agogets ... at least you theoretically can use strcpy securely.