[PATCH] libbb: reduce reliance on -funsigned-char
Daniel Dadap
ddadap at nvidia.com
Wed Mar 12 15:53:11 UTC 2025
The default CFLAGS assigned by the build system include -funsigned-char,
so that certain arithmetic operations on buffer contents can behave as
expected. However, when setting CFLAGS on the make command line, these
defaults get overridden, and if one neglects to include -funsigned-char
in the override CFLAGS, some applets such as `sum` may produce results
which are incorrect.
Update the declaration of the bb_common_bufsiz1 buffer, which is used by
`sum` and others, to explicitly make it a buffer of unsigned char. This
should be harmless, since this is the case anyway with -funsigned-char.
Several other pointers, which are used to do pointer arithmetic against
bb_common_bufsiz1, are also updated to be declared unsigned char.
With this change, `sum` now produces correct results regardless of the
presence of the -funsigned-char CFLAG. However, since there may still be
other implicit dependencies on unsigned char behavior, -funsigned-char
remains in the default CFLAGS assignment.
Signed-off-by: Daniel Dadap <ddadap at nvidia.com>
---
libbb/common_bufsiz.c | 6 +++---
miscutils/fbsplash.c | 2 +-
miscutils/tree.c | 2 +-
procps/nmeter.c | 4 ++--
scripts/generate_BUFSIZ.sh | 4 ++--
5 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/libbb/common_bufsiz.c b/libbb/common_bufsiz.c
index 6bc6d7bc9..d7577fa9d 100644
--- a/libbb/common_bufsiz.c
+++ b/libbb/common_bufsiz.c
@@ -50,7 +50,7 @@
/* We use it for "global" data via *(struct global*)bb_common_bufsiz1.
* Since gcc insists on aligning struct global's members, it would be a pity
* (and an alignment fault on some CPUs) to mess it up. */
-char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long));
+unsigned char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long));
#else
@@ -68,7 +68,7 @@ char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long));
* It is not defined as a dummy macro.
* It means we have to provide this function.
*/
-char *const bb_common_bufsiz1 __attribute__ ((section (".data")));
+unsigned char *const bb_common_bufsiz1 __attribute__ ((section (".data")));
void setup_common_bufsiz(void)
{
if (!bb_common_bufsiz1)
@@ -77,7 +77,7 @@ void setup_common_bufsiz(void)
# else
# ifndef bb_common_bufsiz1
/* bb_common_bufsiz1[] is not aliased to _end[] */
-char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long));
+unsigned char bb_common_bufsiz1[COMMON_BUFSIZE] ALIGNED(sizeof(long long));
# endif
# endif
diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c
index 2934d8eb7..576546f5f 100644
--- a/miscutils/fbsplash.c
+++ b/miscutils/fbsplash.c
@@ -375,7 +375,7 @@ static void fb_drawprogressbar(unsigned percent)
static void fb_drawimage(void)
{
FILE *theme_file;
- char *read_ptr;
+ unsigned char *read_ptr;
unsigned char *pixline;
unsigned i, j, width, height, line_size;
diff --git a/miscutils/tree.c b/miscutils/tree.c
index 0b142c85c..3b58edc04 100644
--- a/miscutils/tree.c
+++ b/miscutils/tree.c
@@ -23,7 +23,7 @@
#define prefix_buf bb_common_bufsiz1
-static void tree_print(unsigned count[2], const char* directory_name, char* prefix_pos)
+static void tree_print(unsigned count[2], const char* directory_name, unsigned char* prefix_pos)
{
struct dirent **entries;
int index, size;
diff --git a/procps/nmeter.c b/procps/nmeter.c
index dca07eac6..edce265ea 100644
--- a/procps/nmeter.c
+++ b/procps/nmeter.c
@@ -87,7 +87,7 @@ struct globals {
// 1 if sample delay is not an integer fraction of a second
smallint need_seconds;
char final_char;
- char *cur_outbuf;
+ unsigned char *cur_outbuf;
int delta;
unsigned deltanz;
struct timeval tv;
@@ -136,7 +136,7 @@ static void print_outbuf(void)
static void put(const char *s)
{
- char *p = cur_outbuf;
+ unsigned char *p = cur_outbuf;
int sz = outbuf + COMMON_BUFSIZE - p;
while (*s && --sz >= 0)
*p++ = *s++;
diff --git a/scripts/generate_BUFSIZ.sh b/scripts/generate_BUFSIZ.sh
index 718788e0b..fae7d68a7 100755
--- a/scripts/generate_BUFSIZ.sh
+++ b/scripts/generate_BUFSIZ.sh
@@ -28,7 +28,7 @@ generate_std_and_exit() {
$debug && echo "Configuring: bb_common_bufsiz1[] in bss"
{
echo "enum { COMMON_BUFSIZE = 1024 };"
- echo "extern char bb_common_bufsiz1[];"
+ echo "extern unsigned char bb_common_bufsiz1[];"
echo "#define setup_common_bufsiz() ((void)0)"
} | regenerate "$common_bufsiz_h"
echo "std" >"$common_bufsiz_h.method"
@@ -39,7 +39,7 @@ generate_big_and_exit() {
$debug && echo "Configuring: bb_common_bufsiz1[] in bss, COMMON_BUFSIZE = $1"
{
echo "enum { COMMON_BUFSIZE = $1 };"
- echo "extern char bb_common_bufsiz1[];"
+ echo "extern unsigned char bb_common_bufsiz1[];"
echo "#define setup_common_bufsiz() ((void)0)"
} | regenerate "$common_bufsiz_h"
echo "$2" >"$common_bufsiz_h.method"
--
2.39.5
More information about the busybox
mailing list