🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

4 total messages Started by MitchAlsup Sun, 11 Oct 2026 19:45
Re: Dodging undefined behaviour in printf
#230
Author: 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.
Re: Dodging undefined behaviour in printf
#248
Author: Keith Thompson
Date: Sun, 11 Oct 2026 23:01
36 lines
2511 bytes
MitchAlsup <user5857@newsgrouper.org.invalid> writes:
> highcrew <high.crew3868@fastmail.com> posted:
[...]
>> 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.

That's incorrect.

An operand of any integer type narrower than signed/unsigned int is
converted to (signed) int if int can represent all values of the
original type, or to unsigned int otherwise.

The integer promotions never convert a value to unsigned long int.

On typical modern 32-bit and 64-bit implementations, short is 16
bits and int is 32 bits.  unsigned short on such implementations
is always promoted to signed int.  unsigned short would only be
promoted to unsigned int in implementations where short and int are
the same size (it's not *quite* that simple).  Such implementations

[...]

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Re: Dodging undefined behaviour in printf
#255
Author: David Brown
Date: Mon, 12 Oct 2026 04:38
87 lines
5001 bytes
On 11/10/2026 21:45, MitchAlsup wrote:
> 
> 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.

That is simply incorrect.

If you have a value of an integer type with rank less than that of "int" 
(and "short", signed or unsigned, always has lower rank than "int" 
regardless of the actual bit-widths) then whenever an "int" or "unsigned 
int" could be used, the smaller type is "promoted".  If an "int" can 
represent all values of the original type, the promotion is to "int" - 
otherwise it is to "unsigned int".  Integer promotion is never to "long" 
types (signed or unsigned).  These rules are in the C standards, and are 
not dependent on sizes.

So if an "int" can represent all the values of "unsigned short", then 
"x" is promoted to "int".  This is the case on most 32-bit and 64-bit 
systems (I think early Cray machines are the only known exceptions, as 
"short", "int" and "long" types were all 64-bit).  On systems with 
16-bit "int", "unsigned short" values will be promoted to "unsigned int" 
- changing type despite remaining the same size.

The sign of the /value/ will not change during integer promotion - the 
signedness of the /type/ typically does (if the original short or char 
was an unsigned type) on 32-bit and 64-bit systems.

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

It is entirely clear - "the integer promotions preserve value including 
sign."  (That is a direct quotation from the standard.)

> 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).
> 

Instruction choice is irrelevant - other than that the compiler must 
choose instructions that give the correct standards-mandated behaviour.

> 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.
>   

Some ABIs use register operands for variadic functions too.  But again, 
the details are irrelevant.

>> 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.

That is good advice.  In this particular case, the OP is not lying to 
his variadic function.  For values that are in the intersection of the 
ranges of "int" and "unsigned int", the signedness of the int type does 
not matter for variadic arguments.  (There are many other circumstances 
in C where it does matter, but not for variadic arguments.)

Re: Dodging undefined behaviour in printf
#268
Author: James Kuyper
Date: Mon, 12 Oct 2026 15:32
40 lines
3137 bytes
On 2026-10-11 15:45, MitchAlsup wrote:
> 
> highcrew <high.crew3868@fastmail.com> posted:
...>> 	unsigned short x = value;
>> 	printf("%x", x);
...> 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.

"if an int can represent all values of the original type (as restricted
by the width, for a bit-field), the value is converted to an int;50)
otherwise, it is converted to an unsigned int. These are called the
integer promotions. All other types are unchanged by the integer
promotions." (6.3.2.1p2)

Note that the first appearance of "integer promotions" is in italics, an
ISO convention indicating that the sentence in which it appears is the
official definition of that term.

Thus, if UCHAR_MAX is less than INT_MAX, unsigned char will promote to
int. If USHRT_MAX < INT_MAX, unsigned short will also promote to int.
Note that it is always the range of representable values that matters,
not the size of the type; integer types are allowed to have padding
bits, so the size is not sufficient to determine the range.

The integer promotions never convert any other type to a long type.


>> 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).

"The integer promotions preserve value including sign." (6.3.2.1p4). Any
implementation that does what you say would be non-conforming.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads