Welcome! Log In Create A New Profile

Advanced

[unit] Compile with -funsigned-char

Anonymous User
September 24, 2024 11:00AM
details: https://github.com/nginx/unit/commit/355f038fdea54bb62fab942729b9386591d308b2
branches: master
commit: 355f038fdea54bb62fab942729b9386591d308b2
user: Andrew Clayton <a.clayton@nginx.com>
date: Thu, 10 Nov 2022 19:54:05 +0000
description:
Compile with -funsigned-char

Due to 'char' (unless explicitly set) being signed or unsigned depending
on architecture, e.g on x86 it's signed, while on Arm it's unsigned,
this can lead to subtle bugs such if you use a plain char as a byte
thinking it's unsigned on all platforms (maybe you live in the world of
Arm).

What we can do is tell the compiler to treat 'char' as unsigned by
default, thus it will be consistent across platforms. Seeing as most of
the time it doesn't matter whether char is signed or unsigned, it
really only matters when you're dealing with 'bytes', which means it
makes sense to default char to unsigned.

The Linux Kernel made this change at the end of 2022.

This will also allow in the future to convert our u_char's to char's
(which will now be unsigned) and pass them directly into the libc
functions for example, without the need for casting.

Here is what the ISO C standard has to say

From §6.2.5 Types ¶15

The three types char, signed char, and unsigned char are collectively
called the character types. The implementation shall define char to
have the same range, representation, and behavior as either signed
char or unsigned char.[45]

and from Footnote 45)

CHAR_MIN, defined in <limits.h>, will have one of the values 0 or
SCHAR_MIN, and this can be used to distinguish the two options.
Irrespective of the choice made, char is a separate type from the
other two and is not compatible with either.

If you're still unsure why you'd want this change...

It was never clear to me, why we used u_char, perhaps that was used as
an alternative to -funsigned-char...

But that still leaves the potential for bugs with char being unsigned vs
signed...

Then because we use u_char but often need to pass such things into libc
(and perhaps other functions) which normally take a 'char' we need to
cast these cases.

So this change brings at least two (or more) benefits

1) Removal of potential for char unsigned vs signed bugs.

2) Removal of a bunch of casts. Reducing casting to the bare minimum
is good. This helps the compiler to do proper type checking.

3) Readability/maintainability, everything is now just char...

What if you want to work with bytes?

Well with char being unsigned (everywhere) you can of course use char.

However it would be much better to use the uint8_t type for that to
clearly signify that intention.

Link: https://lore.kernel.org/lkml/Y1Bfg06qV0sDiugt@zx2c4.com/
Link: https://lore.kernel.org/lkml/20221019203034.3795710-1-Jason@zx2c4.com/
Link: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=3bc753c06dd02a3517c9b498e3846ebfc94ac3ee
Link: <https://www.iso-9899.info/n1570.html#6.2.5p15>
Suggested-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>

---
auto/cc/test | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/auto/cc/test b/auto/cc/test
index 34e4379e..ac4d5f33 100644
--- a/auto/cc/test
+++ b/auto/cc/test
@@ -67,6 +67,8 @@ case $NXT_CC_NAME in

NXT_CFLAGS="$NXT_CFLAGS -fno-strict-overflow"

+ NXT_CFLAGS="$NXT_CFLAGS -funsigned-char"
+
NXT_CFLAGS="$NXT_CFLAGS -std=gnu11"

NXT_CFLAGS="$NXT_CFLAGS -O"
@@ -109,6 +111,8 @@ case $NXT_CC_NAME in

NXT_CFLAGS="$NXT_CFLAGS -fno-strict-overflow"

+ NXT_CFLAGS="$NXT_CFLAGS -funsigned-char"
+
NXT_CFLAGS="$NXT_CFLAGS -std=gnu11"

NXT_CFLAGS="$NXT_CFLAGS -O"
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

[unit] Compile with -funsigned-char

Anonymous User 94 September 24, 2024 11:00AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 110
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready