Menu

#4002 No diagnostic message when subtracting 0 from a pointer to an _Optional type

open
nobody
None
other
5
2026-06-06
2026-06-06
No

1: Sample code that reproduces the problem.

int *green(_Optional int *poi)
{
  return poi - 0; // recommended diagnostic 
}

2: Exact command used to run SDCC on this sample code

sdcc --stack-auto -c green.c

3: SDCC version tested (type "sdcc -v" to find it)

SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/r4k/r5k/r6k/sm83/tlcs90/ez80/z80n/r800/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502/mos65c02/f8/f8l TD- 4.5.24 #16456 (Mac OS X ppc)

4: Copy of the error message or incorrect output, or a clear description of the observed versus expected behavior.

The above example is taken from the Recommended Practice part of subsection 6.5.2 "Type qualifiers" in the _Optional TS.

Implementations that perform data-flow analysis are encouraged to produce a diagnostic
message if one operand has type pointer to optional-qualified type, the other operand has integer type, the operands are evaluated, and analysis cannot prove that no path exists on which the value of the pointer operand is a null pointer.
A diagnostic is encouraged regardless of whether the expression that is added to or subtracted from a pointer is an integer constant expression with value zero, because the referenced type of the result of the + or - operator is not optional-qualified.

SDCC does not produce the recommended diagnostic message about the arithmetic operation, which makes implicit removal of the _Optional qualifier from the referenced type unsafe.

Discussion


Log in to post a comment.

Auth0 Logo