classification
Title: gcc7 throws warning when pymem.h development header is used
Type: Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: [2.7] Fix -Wnonnull and -Wint-in-bool-context warnings
View: 31474
Assigned To: Nosy List: Gabriel Somlo, cstratak, petr.viktorin, vstinner
Priority: normal Keywords: patch

Created on 2017-07-24 14:15 by Gabriel Somlo, last changed 2018-02-28 17:55 by ned.deily. This issue is now closed.

Files
File name Uploaded Description Edit
Python-2.7.13-pymem-gcc7.patch Gabriel Somlo, 2017-07-24 14:15 patch to fix gcc7 warnings when calling PyMem_MALLOC macros
netnsmodule.c Gabriel Somlo, 2017-07-24 14:19 file showing warning emitted with -Wall (error -Werror)
foo.c Gabriel Somlo, 2017-07-24 14:36
Messages (6)
msg298973 - (view) Author: Gabriel Somlo (Gabriel Somlo) * Date: 2017-07-24 14:15
C programs using PyMem_MALLOC in pymem.h generate a warning when -Wint-in-bool-context is enabled (typically through -Wall). In places where -Werror is enabled (such as RPM build), this causes the build to fail with an error that looks like:

...
In file included from /usr/include/python2.7/Python.h:78:0,
                 from netnsmodule.c:16:
netnsmodule.c: In function 'netns_nsexecvp':
/usr/include/python2.7/pymem.h:97:30: error: '*' in boolean context, suggest '&&' instead [-Werror=int-in-bool-context]
  ( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )
                              ^
/usr/include/python2.7/pymem.h:75:15: note: in definition of macro 'PyMem_MALLOC'
     : malloc((n) ? (n) : 1))
               ^
netnsmodule.c:61:10: note: in expansion of macro 'PyMem_NEW'
   argv = PyMem_NEW(char *, argc + 1);
          ^~~~~~~~~
cc1: all warnings being treated as errors
error: command 'gcc' failed with exit status 1
...

I'm attaching a patch that fixes the issue, please consider applying!

Thanks,
--Gabriel
msg298974 - (view) Author: Gabriel Somlo (Gabriel Somlo) * Date: 2017-07-24 14:19
This attachment illustrates how the problem is triggered. The file is part of the CORE network emulator (github.com/coreemu/core). Compile with "gcc -Wall -I/usr/include/python2.7 -c netnsmodule.c".
msg298975 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-07-24 14:25
> /usr/include/python2.7/pymem.h:97:30: error: '*' in boolean context, suggest '&&' instead [-Werror=int-in-bool-context]

I don't understand this error. I would be curious to see the output of the preprocessor. Try maybe to use gcc -E?
msg298977 - (view) Author: Gabriel Somlo (Gabriel Somlo) * Date: 2017-07-24 14:36
output of "gcc -E -Wall -I/usr/include/python2.7 -c netnsmodule.c > foo.c"

I think gcc7 is a bit more paranoid about whether some expression evaluating to an int can/should in itself be used as a Boolean (i.e., without being compared to 0).
msg298978 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2017-07-24 14:42
> malloc(((argc + 1) * sizeof(char *)) ? ((argc + 1) * sizeof(char *)) : 1)) ) );

Ah ok, the "(n)?" expression is "((argc + 1) * sizeof(char *)) ? " and yes it contains "*".
msg313057 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2018-02-28 11:56
This is a duplicate of https://bugs.python.org/issue31474
which was fixed in https://github.com/python/cpython/pull/3581
It should be included in the next Python 2.7.x release.
History
Date User Action Args
2018-02-28 17:55:45ned.deilysetstatus: open -> closed
superseder: [2.7] Fix -Wnonnull and -Wint-in-bool-context warnings
resolution: duplicate
stage: resolved
2018-02-28 11:56:28petr.viktorinsetnosy: + petr.viktorin
messages: + msg313057
2017-07-24 16:17:25cstrataksetnosy: + cstratak
2017-07-24 14:42:29vstinnersetmessages: + msg298978
2017-07-24 14:36:47Gabriel Somlosetfiles: + foo.c

messages: + msg298977
2017-07-24 14:25:29vstinnersetmessages: + msg298975
2017-07-24 14:23:52vstinnersetnosy: + vstinner
2017-07-24 14:19:05Gabriel Somlosetfiles: + netnsmodule.c

messages: + msg298974
2017-07-24 14:15:36Gabriel Somlocreate