Newlib bug report

This forum is for general developer support questions.
Post Reply
User avatar
rwo
Posts: 15
Joined: Thu Mar 10, 2011 11:03 am
Location: Denmark

Newlib bug report

Post 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
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 530
Joined: Sat Jun 18, 2011 3:12 pm
Location: Finland
Contact:

Re: Newlib bug report

Post 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.
User avatar
rwo
Posts: 15
Joined: Thu Mar 10, 2011 11:03 am
Location: Denmark

Re: Newlib bug report

Post 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()
Post Reply