...
Code Block |
---|
void append(char *buf, size_t count, size_t size) { char *line = " <- THIS IS A LINE"; int line_len = strlen(line); if ((count + line_len) > size) buf = realloc(buf,count+line_len); strcat(buf,line); } |
Compliant Solution 2
Correcting the above example is an exercise in documentation. Since realloc is used to resize the memory pointed to by buf, the function append has the precondition that buf must point to dynamically allocated memory.
Code Block |
---|
/\* NOTE: buf must point to dynamically allocated memory \*/ void append(char \*buf, size_t count, size_t size) { char *line = " <- THIS IS A LINE"; int line_len = strlen(line); if ((count + line_len) > size) buf = realloc(buf,count+line_len); strncat(buf,line); } |
References
Seacord 05 Chapter 4 Dynamic Memory Management
MIT Kerberos 5 [http://web.mit.edu/kerberos/advisories/MITKRB5-SA-2004-002-dblfree.txt