AmigaLabs / clib4

Clib4 for AmigaOS4
Other
14 stars 7 forks source link

SPRINTF is adding a superfluous NULL character #208

Open 3246251196 opened 4 weeks ago

3246251196 commented 4 weeks ago

Test program:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
  char stack[64];
  memset(stack, 'z', 64);

  stack[0] = 'p';
  stack[1] = 'e';
  stack[2] = 't';
  stack[3] = 'e';
  stack[4] = 'r';
  stack[5] = ':';

  sprintf(stack+6,"%s,%d:","someFile.c",98);

  {
    int i = 0;
    for (;i<64;++i)
      {
    fprintf(stderr,"%c",stack[i]);
      }
    fprintf(stderr,"\n");
  }

    return 0;
}

Build and run.

main *> outFile

Then, open outFile with a good editor or use hex-mode. Notice the following:

00000000: 70 65 74 65 72 3A 73 6F 6D 65 46 69 6C 65 2E 63  |  peter:someFile.c
00000010: 2C 39 38 3A 00 00 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A  |  ,98:..zzzzzzzzzz
00000020: 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A  |  zzzzzzzzzzzzzzzz
00000030: 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A 7A  |  zzzzzzzzzzzzzzzz
00000040: 0A                                               |  .               

0x015 should not be \0.

This does not happen with Linux, newlib or clib2.

I can see that in vsprintf.c there is an explicit addition of \0.

giovannicanedoli commented 3 weeks ago

Have you tried using snprintf as an alternative? Like you said, in linux this is working properly so I think this is strange

afxgroup commented 3 weeks ago

i think

    /* Put a \0 at the end */
    if (__putc(__clib4, '\0', (FILE * ) &string_iob, IOBF_BUFFER_MODE_NONE) == EOF) {
        result = EOF;
    }

can be removed from vsprintf since vfprintf is adding it already. Can you try it?