![]() ![]() If you are looking at providing a wrapper of log_out() that can take a printf() style parameter list itself, you can use the variant vasprintf() which takes a va_list as an argument. If you try to use a combination of malloc() and snprintf() to produce correct behavior in all cases, you end up with roughly twice as much code than I have given for asprintf(), and basically reprogram the functionality of asprintf(). Also, it'll cut off the trailing endline character, gluing the next log line to the end of your unsuccessfully written line. When writing to a log file, that is a relatively minor concern, but it has the potential to cut off precisely the information that would have been useful. You would be in deep trouble, because you can't know what number to put into the brackets.Įven if you had used the "safe" variant snprintf(), you would still run the danger that your strings gets truncated. You are writing the string to a random location in memory! There is no allocated memory behind the pointer. The sprintf() code you gave in the question is deeply flawed: This is the minimum effort you can get to construct the string in a secure fashion. If(0 > asprintf(
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |