zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] remove unnecessary checks for NULL in zfree and zsfree
@ 2018-03-01  1:15 Taylor West
  2018-03-01  1:17 ` Taylor West
  0 siblings, 1 reply; 6+ messages in thread
From: Taylor West @ 2018-03-01  1:15 UTC (permalink / raw)
  To: zsh-workers; +Cc: Taylor West

---
 Src/mem.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/Src/mem.c b/Src/mem.c
index f120819..77e4375 100644
--- a/Src/mem.c
+++ b/Src/mem.c
@@ -1885,16 +1885,14 @@ bin_mem(char *name, char **argv, Options ops, int func)
 mod_export void
 zfree(void *p, UNUSED(int sz))
 {
-    if (p)
-	free(p);
+    free(p);
 }
 
 /**/
 mod_export void
 zsfree(char *p)
 {
-    if (p)
-	free(p);
+    free(p);
 }
 
 /**/
-- 
2.9.5


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] remove unnecessary checks for NULL in zfree and zsfree
  2018-03-01  1:15 [PATCH] remove unnecessary checks for NULL in zfree and zsfree Taylor West
@ 2018-03-01  1:17 ` Taylor West
  2018-03-01  9:15   ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Taylor West @ 2018-03-01  1:17 UTC (permalink / raw)
  To: zsh-workers

[-- Attachment #1: Type: text/plain, Size: 977 bytes --]

According to section 7.22.3.3 paragraph 2 of the final committee draft
of the C11 standard, if free() is passed a NULL pointer then "no
action occurs". The current checks on `p' in zfree and zsfree are
unnecessary---this patch removes them.

________________________________
From: Taylor West <KrokodileGlue@outlook.com>
Sent: Wednesday, February 28, 2018 5:15 PM
To: zsh-workers@zsh.org
Cc: Taylor West
Subject: [PATCH] remove unnecessary checks for NULL in zfree and zsfree

---
 Src/mem.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/Src/mem.c b/Src/mem.c
index f120819..77e4375 100644
--- a/Src/mem.c
+++ b/Src/mem.c
@@ -1885,16 +1885,14 @@ bin_mem(char *name, char **argv, Options ops, int func)
 mod_export void
 zfree(void *p, UNUSED(int sz))
 {
-    if (p)
-       free(p);
+    free(p);
 }

 /**/
 mod_export void
 zsfree(char *p)
 {
-    if (p)
-       free(p);
+    free(p);
 }

 /**/
--
2.9.5


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] remove unnecessary checks for NULL in zfree and zsfree
  2018-03-01  1:17 ` Taylor West
@ 2018-03-01  9:15   ` Peter Stephenson
  2018-03-01 13:29     ` Taylor West
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2018-03-01  9:15 UTC (permalink / raw)
  To: zsh-workers

On Thu, 1 Mar 2018 01:17:12 +0000
Taylor West <krokodileglue@outlook.com> wrote:
> According to section 7.22.3.3 paragraph 2 of the final committee draft
> of the C11 standard, if free() is passed a NULL pointer then "no
> action occurs". The current checks on `p' in zfree and zsfree are
> unnecessary---this patch removes them.

We haven't even progressed to C99 yet... zsh is designed to be defensive
about some pretty old stuff.  Unfortunately, it's quite difficult to
know when where it's still being compiled.

pws


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] remove unnecessary checks for NULL in zfree and zsfree
  2018-03-01  9:15   ` Peter Stephenson
@ 2018-03-01 13:29     ` Taylor West
  2018-03-01 19:45       ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Taylor West @ 2018-03-01 13:29 UTC (permalink / raw)
  To: Peter Stephenson, zsh-workers

[-- Attachment #1: Type: text/plain, Size: 1270 bytes --]

I quoted the C11 standard because it's the one I'm most familiar with, but that particular clause has been present since the first ANSI draft. In the C89 standard it's section 4.10.3.2: ``If ptr is a null pointer, no action occurs.'' Even if zsh were to rely on some variant of GNU C, no extension that I know of modifies this part of the standard. Unless you know of a particular platform with a particularly bad compiler that you want to support, I recommend taking advantage of these kinds of guarantees.
________________________________
From: Peter Stephenson <p.stephenson@samsung.com>
Sent: Thursday, March 1, 2018 1:15 AM
To: zsh-workers@zsh.org
Subject: Re: [PATCH] remove unnecessary checks for NULL in zfree and zsfree

On Thu, 1 Mar 2018 01:17:12 +0000
Taylor West <krokodileglue@outlook.com> wrote:
> According to section 7.22.3.3 paragraph 2 of the final committee draft
> of the C11 standard, if free() is passed a NULL pointer then "no
> action occurs". The current checks on `p' in zfree and zsfree are
> unnecessary---this patch removes them.

We haven't even progressed to C99 yet... zsh is designed to be defensive
about some pretty old stuff.  Unfortunately, it's quite difficult to
know when where it's still being compiled.

pws

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] remove unnecessary checks for NULL in zfree and zsfree
  2018-03-01 13:29     ` Taylor West
@ 2018-03-01 19:45       ` Peter Stephenson
  2018-03-01 21:37         ` Joey Pabalinas
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2018-03-01 19:45 UTC (permalink / raw)
  To: zsh-workers

On Thu, 1 Mar 2018 13:29:13 +0000
Taylor West <krokodileglue@outlook.com> wrote:
> I quoted the C11 standard because it's the one I'm most familiar with,
> but that particular clause has been present since the first ANSI
> draft.

OK, I would be a bit surprised if there were any pre-ANSI compilers
still in use.  Unless someone tells me they have a SunOS 4.1.1 system
proudly sitting on their desk.

pws



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] remove unnecessary checks for NULL in zfree and zsfree
  2018-03-01 19:45       ` Peter Stephenson
@ 2018-03-01 21:37         ` Joey Pabalinas
  0 siblings, 0 replies; 6+ messages in thread
From: Joey Pabalinas @ 2018-03-01 21:37 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Taylor West, zsh-workers, Joey Pabalinas

[-- Attachment #1: Type: text/plain, Size: 1074 bytes --]

On Thu, Mar 01, 2018 at 07:45:54PM +0000, Peter Stephenson wrote:
> OK, I would be a bit surprised if there were any pre-ANSI compilers
> still in use.  Unless someone tells me they have a SunOS 4.1.1 system
> proudly sitting on their desk.

Yeah, I mean even in the C89 draft the free() function is required to
be a no-op if the pointer is NULL:

> 4.10.3.2 The free function
> 
> The free function causes the space pointed to by ptr to be deallocated,
> that is, made available for further allocation. If ptr is a null pointer,
> no action occurs. Otherwise, if the argument does not match a pointer
> earlier returned by the calloc , malloc , or realloc function, or if
> the space has been deallocated by a call to free or realloc , the
> behavior is undefined.

So I don't see any problems with this patch, personally.

The only times I have ever seen people use pre-ANSI compilers
they were only uses them on pre-ANSI programs; I don't think
anyone expects to be able to compile moderns programs with a
compiler that old.

-- 
Joey Pabalinas

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-03-01 21:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-01  1:15 [PATCH] remove unnecessary checks for NULL in zfree and zsfree Taylor West
2018-03-01  1:17 ` Taylor West
2018-03-01  9:15   ` Peter Stephenson
2018-03-01 13:29     ` Taylor West
2018-03-01 19:45       ` Peter Stephenson
2018-03-01 21:37         ` Joey Pabalinas

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).