Page 1 of 1

Newlib bug report

Posted: Fri Oct 05, 2018 5:54 am
by rwo
Todays bug report is about newlib's snprintf() function

This example should return 0 but returns 4..

// --

#include <proto/dos.h>
#include <proto/Reactive.h>

#include <stdio.h>

int main( void )
{
TEXT buf[64];
int32 v;

buf[0] = 1;
buf[1] = 1;
buf[2] = 1;
buf[3] = 1;

v = snprintf( buf, 1, "1234" );

printf( "v %ld\n", v );
printf( "%d\n", buf[0] );
printf( "%d\n", buf[1] );
printf( "%d\n", buf[2] );
printf( "%d\n", buf[3] );

return( 0 );
}

Result looks like this

> tst.exe
v 4
0
1
1
1

Re: Newlib bug report

Posted: Fri Oct 05, 2018 7:03 am
by salass00
No, it is correct. The snprintf() function always returns the number of characters it would have written had the buffer been large enough rather than how many characters it actually wrote. This is to allow you to easily check if the result was truncated by simply comparing the return value to the size of the buffer you used.

Re: Newlib bug report

Posted: Sun Oct 07, 2018 10:05 am
by rwo
salass00 wrote:No, it is correct. The snprintf() function always returns the number of characters it would have written had the buffer been large enough rather than how many characters it actually wrote. This is to allow you to easily check if the result was truncated by simply comparing the return value to the size of the buffer you used.
ohh ya.. I was looking at sprintf()