Newlib bug report

This forum is for general developer support questions.

Newlib bug report

Postby rwo » Fri Oct 05, 2018 6:54 am

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
User avatar
rwo
 
Posts: 15
Joined: Thu Mar 10, 2011 12:03 pm
Location: Denmark

Re: Newlib bug report

Postby salass00 » Fri Oct 05, 2018 8:03 am

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.
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
 
Posts: 506
Joined: Sat Jun 18, 2011 4:12 pm
Location: Finland

Re: Newlib bug report

Postby rwo » Sun Oct 07, 2018 11:05 am

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()
User avatar
rwo
 
Posts: 15
Joined: Thu Mar 10, 2011 12:03 pm
Location: Denmark


Return to General Developer Support

Who is online

Users browsing this forum: No registered users and 2 guests