Menu ▾ ▴

#4317 Assignment of a pointer to an int goes undiagnosed

open
None
other
5
12 hours ago
16 hours ago
No

1: Sample code that reproduces the problem.

void foo(int *i, int *j)
{
    *i = *i + j;
}

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

sdcc --std-c11 -S example.c

3: SDCC version tested.

SDCC 4.6.5 #17328 (Linux), default MCS51 target, on Compiler Explorer.

4: Observed and expected behaviour.

SDCC exits successfully and emits no diagnostic. The same happens with the default options on Compiler Explorer.

The addition *i + j is valid and its result has type int *. Assigning that result to *i, which has type int, violates the constraints on simple assignment in C11 6.5.16.1 paragraph 1. A diagnostic is required by 5.1.1.3 paragraph 1.

Replacing the whole assignment with *i = j; reproduces the missing diagnostic.

Related

Wiki: SDCC 4.7.0 Release

Discussion

  • Christopher Bazley

    Christopher Bazley - 16 hours ago
    • assigned_to: Christopher Bazley
     
  • Christopher Bazley

    Christopher Bazley - 12 hours ago

    Here's Codex's own account of its questionable choices:

    The first candidate checked the assignment in decorateType, where both operand types are available. It diagnosed the reproducer, but used error 47 (“indirections to different types”). SDCC already has warning 155 (“converting pointer to integral without a cast”), which describes the problem precisely. The patch also changed the handling of _Bool, a separate issue.

    The second candidate addressed the missed warning during iCode generation. For *i = j, the lowered left operand holds the address i. checkTypes compares that address type with the type of j, sees two int * types and skips the conversion. The patch recovered the destination type from the AST specifically for pointer-to-integer assignments.

    The third candidate removed that special case by passing the destination type from the AST to checkTypes for every simple assignment. This worked, but required changes to several iCode functions to enforce a constraint that can already be checked in the AST.

    The current candidate represents the implicit conversion in the AST, following the existing approach for return statements. The cast checker then emits warning 155 before lowering.

    I did not attach the first three candidate patches but I think the final candidate is a good patch, therefore I am attaching it. Project rules say that someone else must also approve it. It would be great to get this fix in the 4.7.0 release; I found the lack of any diagnostic message surprising and worrying.

    Regression test results:

    Summary for 'ucz80'                  :      0 failures, 37672 tests, 6582 test cases,  7540450 bytes, 1543020228 ticks
    

    Valdiag test results:

    Summary for 'mcs51'                  :      0 failures,  1977 tests, 1487 test cases,        0 bytes,          0 ticks
    
     

Log in to post a comment.