bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] non-null declarations


From: Bruno Haible
Subject: Re: [PATCH] non-null declarations
Date: Fri, 11 Dec 2009 00:50:56 +0100
User-agent: KMail/1.9.9

Hi Dmitry,

> Potential drawback of this approach is that these declarations may affect
> the implementation as well: there is a risk that compiler may optimize out
> NULL checks.  In other projects where this issue arose before it was
> worked around by disabling non-null declarations in implementation scope.

I'm glad you mention this. Indeed, the gcc doc says
    "The compiler may also choose to make optimizations based
     on the knowledge that certain function arguments will not be null."

and this sample program

============================ foo.c =======================
int foo (char *p) __attribute__ ((__nonnull__ (1)));

int foo (char *p)
{
  if (!p)
    return 77;
  return p[0] + p[1];
}
==========================================================
is compiled without the NULL test at optimization levels
-O2 or higher.

I'll work around it by adding these changes:


        * lib/canonicalize-lgpl.c (_GL_ARG_NONNULL): Define, to defeat gcc
        optimization.
        * lib/getaddrinfo.c (_GL_ARG_NONNULL): Likewise.
        * lib/getdelim.c (_GL_ARG_NONNULL): Likewise.
        * lib/glob.c (_GL_ARG_NONNULL): Likewise.
        * lib/random_r.c (_GL_ARG_NONNULL): Likewise.
        * lib/setenv.c (_GL_ARG_NONNULL): Likewise.
        * lib/strtod.c (_GL_ARG_NONNULL): Likewise.
        * lib/tsearch.c (_GL_ARG_NONNULL): Likewise.
        * lib/unsetenv.c (_GL_ARG_NONNULL): Likewise.

--- lib/canonicalize-lgpl.c.orig        2009-12-11 00:44:34.000000000 +0100
+++ lib/canonicalize-lgpl.c     2009-12-10 23:54:58.000000000 +0100
@@ -21,6 +21,10 @@
 
 #if !HAVE_CANONICALIZE_FILE_NAME || !FUNC_REALPATH_WORKS || defined _LIBC
 
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   optimizes away the name == NULL test below.  */
+#define _GL_ARG_NONNULL(params)
+
 /* Specification.  */
 #include <stdlib.h>
 
--- lib/getaddrinfo.c.orig      2009-12-11 00:44:34.000000000 +0100
+++ lib/getaddrinfo.c   2009-12-10 23:27:28.000000000 +0100
@@ -1,6 +1,5 @@
 /* Get address information (partial implementation).
-   Copyright (C) 1997, 2001, 2002, 2004, 2005, 2006, 2007, 2008 Free Software
-   Foundation, Inc.
+   Copyright (C) 1997, 2001-2002, 2004-2009 Free Software Foundation, Inc.
    Contributed by Simon Josefsson <address@hidden>.
 
    This program is free software; you can redistribute it and/or modify
@@ -19,6 +18,10 @@
 
 #include <config.h>
 
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   optimizes away the sa == NULL test below.  */
+#define _GL_ARG_NONNULL(params)
+
 #include <netdb.h>
 
 #if HAVE_NETINET_IN_H
--- lib/getdelim.c.orig 2009-12-11 00:44:34.000000000 +0100
+++ lib/getdelim.c      2009-12-10 23:41:00.000000000 +0100
@@ -21,6 +21,10 @@
 
 #include <config.h>
 
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   optimizes away the lineptr == NULL || n == NULL || fp == NULL tests below.  
*/
+#define _GL_ARG_NONNULL(params)
+
 #include <stdio.h>
 
 #include <limits.h>
--- lib/glob.c.orig     2009-12-11 00:44:34.000000000 +0100
+++ lib/glob.c  2009-12-10 23:22:11.000000000 +0100
@@ -19,6 +19,10 @@
 # include <config.h>
 #endif
 
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   optimizes away the pattern == NULL || pglob == NULL tests below.  */
+#define _GL_ARG_NONNULL(params)
+
 #include <glob.h>
 
 #include <errno.h>
--- lib/random_r.c.orig 2009-12-11 00:44:34.000000000 +0100
+++ lib/random_r.c      2009-12-10 23:52:48.000000000 +0100
@@ -1,5 +1,5 @@
 /*
-   Copyright (C) 1995, 2005, 2008 Free Software Foundation, Inc.
+   Copyright (C) 1995, 2005, 2008-2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -52,6 +52,11 @@
 
 #include <config.h>
 
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   optimizes away the buf == NULL, arg_state == NULL, result == NULL tests
+   below.  */
+#define _GL_ARG_NONNULL(params)
+
 #include <errno.h>
 #include <limits.h>
 #include <stddef.h>
--- lib/setenv.c.orig   2009-12-11 00:44:34.000000000 +0100
+++ lib/setenv.c        2009-12-10 23:56:23.000000000 +0100
@@ -17,6 +17,11 @@
 #if !_LIBC
 # include <config.h>
 #endif
+
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   optimizes away the name == NULL test below.  */
+#define _GL_ARG_NONNULL(params)
+
 #include <alloca.h>
 
 /* Specification.  */
--- lib/strtod.c.orig   2009-12-11 00:44:34.000000000 +0100
+++ lib/strtod.c        2009-12-10 23:57:35.000000000 +0100
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991, 1992, 1997, 1999, 2003, 2006, 2008 Free
+/* Copyright (C) 1991-1992, 1997, 1999, 2003, 2006, 2008-2009 Free
    Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
@@ -16,6 +16,10 @@
 
 #include <config.h>
 
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   optimizes away the nptr == NULL test below.  */
+#define _GL_ARG_NONNULL(params)
+
 #include <stdlib.h>
 
 #include <ctype.h>
--- lib/tsearch.c.orig  2009-12-11 00:44:34.000000000 +0100
+++ lib/tsearch.c       2009-12-10 23:30:49.000000000 +0100
@@ -1,4 +1,4 @@
-/* Copyright (C) 1995-1997, 2000, 2006-2007 Free Software Foundation, Inc.
+/* Copyright (C) 1995-1997, 2000, 2006-2007, 2009 Free Software Foundation, 
Inc.
    Contributed by Bernd Schmidt <address@hidden>, 1997.
 
    NOTE: The canonical source of this file is maintained with the GNU C
@@ -86,6 +86,10 @@
 
 #include <config.h>
 
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   optimizes away the rootp == NULL tests below.  */
+#define _GL_ARG_NONNULL(params)
+
 /* Specification.  */
 #ifdef IN_LIBINTL
 # include "tsearch.h"
--- lib/unsetenv.c.orig 2009-12-11 00:44:34.000000000 +0100
+++ lib/unsetenv.c      2009-12-10 23:58:34.000000000 +0100
@@ -16,6 +16,10 @@
 
 #include <config.h>
 
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   optimizes away the name == NULL test below.  */
+#define _GL_ARG_NONNULL(params)
+
 /* Specification.  */
 #include <stdlib.h>
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]