🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #230

Re: Dodging undefined behaviour in printf

#230
From: MitchAlsup
Date: Sun, 11 Oct 2026 19:45
42 lines
2494 bytes
highcrew <high.crew3868@fastmail.com> posted:

> Dear c.l.c.
> 
> I'm trying to refine my knowledge on the topic of type promotions, and 
> now I know how promotion is implied when passing arguments to a function 
> whose prototype is missing argument definition, or to functions having 
> variadic argument list.
> 
> So I was wondering about the following:
> 
> 	unsigned short x = value;
> 	printf("%x", x);
> 
> Assuming that sizeof(unsigned short) < sizeof(int), I would guess that x 
> is promoted to signed int, which is however the wrong type, as it should 
> be unsigned int.

On a 64-bit machine, x gets promoted to unsigned long int.
On a 32-bit machine, x gets promoted to unsigned int.

signedness is not changed during promotion.

> Well, it is is also true that the `int` value is not going to be 
> negative for sure.

Unclear !! Depending on ABI, the compiler might LD x using a LDUH instruction
and by the time it gets to a register is "has already been promoted". As
most ABIs pas at least a few arguments in registers, the LDUH is often all 
that gets done on the calling side. {However, if x is loaded as a short
and then promoted, strange things may happen when the HoB of x is set (1).

On the called side, the arguments are going to be stored in memory in a
linear vector (varargs) so they can be extracted one after another such
that the called side does not need to know how many there are. One gets
a pointer to the linear vector with vastart.
 
> Would this be a problem? Am I supposed to explicitly cast x to (unsigned 
> int) when passing it to printf?

Do not lie to your variable argument functions.
Technical Details
Message-ID: <1791747948-5857@newsgrouper.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!newsgrouper.org!.POSTED!not-for-mail
References: <119bf9d$1fk39$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!newsgrouper.org!.POSTED!not-for-mail
From: MitchAlsup <user5857@newsgrouper.org.invalid>
Newsgroups: comp.lang.c
Subject: Re: Dodging undefined behaviour in printf
References: <119bf9d$1fk39$1@dont-email.me>
Date: Sun, 11 Oct 2026 19:45:48 GMT
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <1791747948-5857@newsgrouper.org>
Injection-Info: newsgrouper.org; mail-complaints-to="newsgrouper@yahoo.com"; posting-account=user5857
Injection-Date: Sun, 11 Oct 2026 19:45:48 GMT
User-Agent: Newsgrouper/0.9.0