Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile with -funsigned-char #1340

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Conversation

ac000
Copy link
Member

@ac000 ac000 commented Jun 24, 2024

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.

@ac000
Copy link
Member Author

ac000 commented Jul 3, 2024

  • Rebased with master
  • Commit message tweak
$ git range-diff 0ab815b8...5eb8f02f
 -:  -------- >  1:  5c4911a3 perl: Constify some local static variables
 -:  -------- >  2:  ab1b3f9d test/clone: Constify some local static variables
 -:  -------- >  3:  8e254a4d tstr, conf: Ensure error strings are nul-terminated
 -:  -------- >  4:  d62a5e2c contrib: updated njs to 0.8.5
 -:  -------- >  5:  a9aa9e76 python: Support application factories
 -:  -------- >  6:  d67d3501 tests: Add tests for python application factories
 -:  -------- >  7:  36213522 Fix certificate deletion for array type certificates
 -:  -------- >  8:  ff6d5045 python: Constify some local static variables
 1:  0ab815b8 !  9:  5eb8f02f Compile with -funsigned-char
    @@ Commit message
     
         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 if you use a plain char as a byte thinking
    -    it's unsigned on all platforms (maybe you live in the world of Arm).
    +    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

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/[email protected]/>
Link: <https://lore.kernel.org/lkml/[email protected]/>
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 <[email protected]>
Reviewed-by: Alejandro Colomar <[email protected]>
Signed-off-by: Andrew Clayton <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant