Skip to content

SPRINTF is adding a superfluous NULL character #208

@3246251196

Description

@3246251196

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions