Thread (6 messages) flat view 6 messages, 4 authors, 2020-01-07

Re: [Outreachy] Return value before or after free()?

From: Jeff King <hidden>
Date: 2020-01-06 21:30:54

On Mon, Jan 06, 2020 at 10:15:53PM +0100, Miriam R. wrote:
in run-command.c file `exists_in_PATH()` function does this:

static int exists_in_PATH(const char *file)
{
char *r = locate_in_PATH(file);
free(r);
return r != NULL;
}

I wonder if it is correct to do return r != NULL; after free(r);
It is technically undefined behavior according to the C standard, but I
think it would be hard to find an implementation where it was not
perfectly fine in practice.

Ref: http://c-faq.com/malloc/ptrafterfree.html

I'd probably leave it alone unless it is causing a problem (e.g., a
static analyzer complaining).

-Peff
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help