Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#18098 closed bug (fixed)

snprintf: different results on 32 and 64 bits.

Reported by: bipolar Owned by: korli
Priority: normal Milestone: R1/beta4
Component: System/libroot.so Version: R1/beta4
Keywords: Cc:
Blocked By: Blocking:
Platform: All

Description (last modified by bipolar)

While running the wget2 test-suite, Begasus found some failing tests (see https://github.com/haikuports/haikuports/pull/7484).

I've managed to distill/recreate the failing test cases with the following test_snprintf small program:

#include <stdio.h>

int main(void)
{
	char* fmts[]={"%0lld","%0lli","%0llu","%00lld","%00lli", "%00llu"};
	char result[32];

	int i;
	for (i=0; i < 6; i++) {
		// Without using "(long long int)0" here, the 32 bits versions output a different
		// big number (like "141014943242649600") on each run, instead of "".
		// On 64 bits, using just 0, works ok (output is "0").
		snprintf(result, sizeof(result), fmts[i], (long long int)0);
		printf("(%s, 0) - Expected: '0' got: '%s'\n", fmts[i], result);
	}

	return 0;
}

Output on 32 bits (on both gcc2 and gcc11):

> test_snprintf
(%0lld, 0) - Expected: '0' got: ''
(%0lli, 0) - Expected: '0' got: ''
(%0llu, 0) - Expected: '0' got: ''
(%00lld, 0) - Expected: '0' got: ''
(%00lli, 0) - Expected: '0' got: ''
(%00llu, 0) - Expected: '0' got: ''

Output on 64 bits:

> test_snprintf
(%0lld, 0) - Expected: '0' got: '0'
(%0lli, 0) - Expected: '0' got: '0'
(%0llu, 0) - Expected: '0' got: '0'
(%00lld, 0) - Expected: '0' got: '0'
(%00lli, 0) - Expected: '0' got: '0'
(%00llu, 0) - Expected: '0' got: '0'

Not sure about how important this difference in behavior might be (or if I'm also messing things up in that test), but...

At least this is easy to reproduce.

(just to be clear, the wget2 test-suite expects the behavior seen on the 64 bits version, thus my use of "Expected: '0'").

Edit: Just in case... original wget2 test function: https://gitlab.com/gnuwget/wget2/-/blob/master/unit-tests/test.c#L326 that has the failing cases on 32 bits.

Change History (6)

comment:1 by bipolar, 2 years ago

Description: modified (diff)

comment:2 by korli, 2 years ago

Component: - GeneralSystem/libroot.so
Milestone: UnscheduledR1/beta4
Owner: changed from nobody to korli
Status: newassigned

comment:3 by korli, 2 years ago

Applied in hrev56607 for master.

comment:4 by korli, 2 years ago

Milestone: R1/beta4R1/beta5
Resolution: fixed
Status: assignedclosed

comment:6 by korli, 2 years ago

Milestone: R1/beta5R1/beta4
Note: See TracTickets for help on using tickets.