Menu

#3471 pointer test corrupts value

closed-fixed
None
Z80
5
2022-11-07
2022-09-02
Under4Mhz
No

The following code seems to set the variable start incorrectly to 127. If I remove the any of the if statements, the start value is set correctly to 2.

/// GPL 2.0 or later
#include <string.h>
#include <stdio.h>
#include <stdint.h>

const char TextPunc[] = ":%! ";

#define TEXT_SPACE ( TEXT_PUNC + 3 )
#define TEXT_UPPER ( TEXT_NUM + 10 )
#define TEXT_PUNC ( TEXT_UPPER + 26 )
#define TEXT_NUM ( TEXT_START + 0 )
#define TEXT_START 0

void ImageCompleteText( uint8_t x, uint8_t y, const char *text ) {

    while( *text ) {

        uint8_t ch = *text++;

        uint8_t index = 0;
        uint8_t start = TEXT_SPACE;

        if ( ch == ' ' ) ;
        else{

            char *ptr = strchr( TextPunc, ch );
            if ( ptr )
            {
                index = ptr - TextPunc;
                start = TEXT_PUNC;
            }
        }

        printf( "%d %d %c\n", index, start, ch );

        x += 2;
    }
}


void main() {

    printf("Start\n");
    ImageCompleteText(0,0,"!");
    printf("End\n");
}


#ifdef __SDCC
__sfr __at 0xff sif;
int putchar( int c ) {

    sif = 'p';
    sif = c;

    return c;
}
#endif
sdcc -mz80 ./strchr.c  -o strchr.ihx && ucsim_z80 -I if=outputs[0xff] strchr.ihx
./strchr.c:5: warning 85: in function ImageCompleteText unreferenced function argument : 'y'
./strchr.c:40: warning 283: function declarator with no prototype
uCsim 0.7.5, Copyright (C) 1997 Daniel Drotos.
uCsim comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
0> Loading from strchr.ihx
3260 words read from strchr.ihx
r
Simulation started, PC=0x000000
Start
127 36 !
End
$ gcc ./strchr.c  && ./a.out 
Start
2 36 !
End
$ sdcc -v
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502 4.2.8 #13684 (Linux)

Discussion

  • Philipp Klaus Krause

    I've created a test case from your code (attached).

    But I can't reproduce the issue in current trunk on my Debian GNU/Linux testing system. Can you post the asm code, compiled with --fverbose-asm?

     
    • Under4Mhz

      Under4Mhz - 2022-09-05

      Attached

       
  • Philipp Klaus Krause

    I've been able to reproduce the issue by building sdcc without treedec.

     
  • Philipp Klaus Krause

    • assigned_to: Philipp Klaus Krause
    • Category: other --> Z80
     
  • Philipp Klaus Krause

    • status: open --> closed-fixed
     
  • Philipp Klaus Krause

    Fixed in [r13695].

     

    Related

    Commit: [r13695]

  • Job Bolle

    Job Bolle - 2022-11-01

    The regression test for this bug fails (for me) without treedec with --max-allocs-per-node between 38142 and 170117.

     
    • Philipp Klaus Krause

      It fails for me (Debian GNU/Testing on amd64) too for --max-allocs-per-node 500000 and 45142, but not for 39142.

      P.S.: Looks like codegen for builtin strchr corrupts b , which is the upper byte of text at that point.

      P.P.S.: I have attached a first attempt at a patch (it isn't working yet, gives an error a compilet time)

       

      Last edit: Philipp Klaus Krause 2022-11-01
    • Philipp Klaus Krause

      This should be fixed in [r13745]. Please check if it works for you.

       

      Related

      Commit: [r13745]

      • Job Bolle

        Job Bolle - 2022-11-07

        The regression test no longer fails.
        (It's interesting that the fastest and smallest result is for MAPN=1024.)

        r13744:
        
        MAPN=1024   Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1156 bytes, 23058 ticks
        MAPN=2048   Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1157 bytes, 23075 ticks
        MAPN=4096   Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1159 bytes, 23083 ticks
        MAPN=8192   Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1158 bytes, 23071 ticks
        MAPN=16384  Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1158 bytes, 23071 ticks
        MAPN=32768  Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1158 bytes, 23071 ticks
        MAPN=65536  Summary for 'ucz80': 255 failures, 261 tests, 1 test cases, 1153 bytes, 4806152 ticks   Failure: bug-3471.c
        MAPN=131072 Summary for 'ucz80': 255 failures, 264 tests, 1 test cases, 1152 bytes, 4805526 ticks   Failure: bug-3471.c
        MAPN=262144 Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1152 bytes, 23047 ticks
        MAPN=524288 Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1152 bytes, 23047 ticks
        MAPN=1048576    Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1152 bytes, 23047 ticks
        
        
        r13745:
        
        MAPN=1024   Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1156 bytes, 23058 ticks
        MAPN=2048   Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1157 bytes, 23075 ticks
        MAPN=4096   Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1159 bytes, 23083 ticks
        MAPN=8192   Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1158 bytes, 23071 ticks
        MAPN=16384  Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1158 bytes, 23071 ticks
        MAPN=32768  Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1158 bytes, 23071 ticks
        MAPN=65536  Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1163 bytes, 23147 ticks
        MAPN=131072 Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1162 bytes, 23128 ticks
        MAPN=262144 Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1162 bytes, 23128 ticks
        MAPN=524288 Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1162 bytes, 23128 ticks
        MAPN=1048576    Summary for 'ucz80': 0 failures, 3 tests, 1 test cases, 1162 bytes, 23128 ticks
        
         

Log in to post a comment.