Menu

#4055 stack overflow when using uitoa

open
nobody
None
Z80
5
1 day ago
1 day ago
Under4Mhz
No

The following code causes a stack overflow. If I use the local version of uitoa the problem doesn't happen.

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

#ifdef linux
void __uitoa(uint16_t value, char *str, uint8_t base) {

    sprintf(str, "%u", value);
}
#endif

char *itoa( int16_t value, char *text ) {

    char *ptr = text;

    if ( value < 0 ) {

        *ptr++ = '-';
        value = -value;
    }

    __uitoa( value, ptr, 10 );

    return text;
}

void TimeDraw() {

    char text[5];

    uint8_t mins = 7;
    uint8_t sec = 36;

    // Time
    itoa( mins, text );
    text[1] = ':';
    itoa( sec, text + 2 );
}

void main(void) {

    printf( "Start\n" );

    TimeDraw();

    printf( "End\n" );
}

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

    sif = 'p';
    sif = c;

    return c;
}
#endif
$ sdcc -mz80 ./stack.c  -o stack.ihx && ucsim_z80 -I if=outputs[0xff] stack.ihx
r
Simulation started, PC=0x000000
Start

Stop at 0x00ffed: (106) Invalid instruction 0x00ed
F 0x00ffed

$ gcc ./stack.c && ./a.out
Start
End
$ sdcc -v
SDCC : z80/sm83/ez80/z80n/mos6502/mos65c02 4.6.2 #16701 (Linux)
3 Attachments

Discussion


Log in to post a comment.

MongoDB Logo MongoDB