Opened 14 months ago

Closed 4 months ago

#6327 closed defect (fixed)

Detect misuses of NULL

Reported by: Even Rouault Owned by: Even Rouault
Priority: normal Milestone:
Component: default Version: unspecified
Severity: normal Keywords:
Cc: Kurt Schwehr


If using C++11 nullptr, gcc has a -Wzero-as-null-pointer-constant switch to detect misuses of nullptr.

For now as we haven't switched to C++11, I've implemented a hack that "#define NULL nullptr" when NULL_AS_NULLPTR is defined. This uncovers misues of NULL vs FALSE, or 0 vs NULL.

In a later stage, we could replace all NULL uses in C++ files with nullptr and #define nullptr NULL for non C++11 compilers while we keep C++03 compatibility.

Change History (6)

comment:1 Changed 14 months ago by Even Rouault

trunk r33104 "cpl_port.h: add a hack to '#define NULL nullptr' when NULL_AS_NULLPTR is defined. Must be used together with -std=c++11 -Wzero-as-null-pointer-constant with GCC to detect misuses of NULL/nullptr (#6327)"

trunk r33105 "Replace misuses of 0 or FALSE by NULL (#6327)"

comment:2 Changed 14 months ago by Even Rouault

trunk r33106 "Correct inappropriate NULL vs 0 changes (#6327)" trunk r33107 "Use NULL instead of 0" trunk r33108 "Attempt avoiding problem with = 0 optional parameters in Xerces headers" trunk r33109 "ILI: use NULL instead of 0"

"-Wzero-as-null-pointer-constant -DNULL_AS_NULLPTR" enabled in gdal_coverage/trunk_gcc4.8_stdc11

comment:3 Changed 14 months ago by Kurt Schwehr

+1 for getting to nullptr with #define for older c++ stds.

comment:4 Changed 7 months ago by Kurt Schwehr

I propose that we convert header uses of NULL to CPU_NULL so that can be controlled separately without messing with NULL inside of code using GDAL. I don't know if we want to do that for the entire code base as there are almost 50K lines with NULL.

Bison 3.0.2 does this where YY_NULL -> YY_NULLPTR. e.g. r35135

find . -name \*.h | xargs grep NULL | egrep -v '//|/[*].*NULL ' | wc -l

find . -name \*.cpp | xargs egrep '[^a-zA-Z_0-9]NULL[^a-zA-Z_0-9]' | wc -l

comment:5 Changed 7 months ago by Even Rouault

I'm not really convinced we should change the somewhat standard NULL to a non-standard CPL_NULL if in a next step we switch to C++11, in which case we might directly convert to nullptr.

I'm not sure to understand why we would change that just in .h ?

comment:6 Changed 4 months ago by Even Rouault

Resolution: fixed
Status: newclosed

Closing. This is implemented

Note: See TracTickets for help on using tickets.