Opened 8 years ago

Closed 7 years 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

Description

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 by Even Rouault, 8 years ago

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 by Even Rouault, 8 years ago

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 by Kurt Schwehr, 8 years ago

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

comment:4 by Kurt Schwehr, 8 years ago

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

Error: Failed to load processor bash
No macro or processor named 'bash' found

comment:5 by Even Rouault, 8 years ago

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 by Even Rouault, 7 years ago

Resolution: fixed
Status: newclosed

Closing. This is implemented

Note: See TracTickets for help on using tickets.