awk icon indicating copy to clipboard operation
awk copied to clipboard

Unnecessary Check For Non-NULL Pointer Before free(3) Call

Open ldo opened this issue 3 years ago • 1 comments

The xfree macro checks that the pointer is non-NULL before calling free(3). This is unnecessary. From the man page:

 If ptr is NULL, no operation is performed.

As a general principle, I like to give all my object-freeing routines the same behaviour.

diff --git a/awk.h b/awk.h
index cc30249..ccd16c9 100644
--- a/awk.h
+++ b/awk.h
@@ -37,7 +37,7 @@ typedef double    Awkfloat;

 typedef    unsigned char uschar;

-#define    xfree(a)    { if ((a) != NULL) { free((void *)(intptr_t)(a)); (a) = NULL; } }
+#define    xfree(a)    { free((void *)(intptr_t)(a)); (a) = NULL; }
 /*
  * We sometimes cheat writing read-only pointers to NUL-terminate them
  * and then put back the original value

ldo avatar Aug 24 '22 00:08 ldo

thanks, this is a good catch.

plan9 avatar Aug 25 '22 19:08 plan9