C Programming Project Testing - getting better at

The fix for that is adding a free() after the if
Better: Move the first asprintf in the else branch. There is no point to run that statement in case of an error message:
C:
if(memcmp("*ERROR*", ret, 7) == 0)
   asprintf(&message, "[%d] %s : %s - \"%s\"", tests_run, ret, label, errmsg);
else
   asprintf(&message, "[%d] %s : %s", tests_run, ret, label);

Then the existing free(message); will free either of them.

Even better:
C:
if(memcmp("*ERROR*", ret, 7) != 0)
   asprintf(&message, "[%d] %s : %s", tests_run, ret, label);
else
   asprintf(&message, "[%d] %s : %s - \"%s\"", tests_run, ret, label, errmsg);

Since non-error messages are (hopefully) more common, it makes sense to switch the branches.
 
Better: Move the first asprintf in the else branch. There is no point to run that statement in case of an error message:
C:
if(memcmp("*ERROR*", ret, 7) == 0)
   asprintf(&message, "[%d] %s : %s - \"%s\"", tests_run, ret, label, errmsg);
else
   asprintf(&message, "[%d] %s : %s", tests_run, ret, label);

Then the existing free(message); will free either of them.

Even better:
C:
if(memcmp("*ERROR*", ret, 7) != 0)
   asprintf(&message, "[%d] %s : %s", tests_run, ret, label);
else
   asprintf(&message, "[%d] %s : %s - \"%s\"", tests_run, ret, label, errmsg);

Since non-error messages are (hopefully) more common, it makes sense to switch the branches.
Nice! ...I'm not at my laptop (around the code at the moment) but I was also thinking that "ret" needed freeing so I *think* I free'd "ret" as well as "message". But I will come back to this later when I can. Thank you.
 
> [...] I've sort of started to use this thread as diary of sorts now. ...I'll stop. [...]
You'll see which kind of posts give you feedback and which do not. When you encounter a problem, please do not hesitate to post it here, because as a wrote above: chances are good that you get help quickliy.
Ah. Well (honestly), I probably will not. I'm not really looking for popularity or clout just conversation/learning/perspectives/etc. and I've been around enough forums and whatnot to know more people than the OP learns from a post/thread so I don't follow "post-formulas" in that regard (-e.g. if anyone has an opinion--the capacity of thought, even--and a decent amount of common courtesy, they can feel free to answer any of my threads).

l8r
 
Back
Top