Skip to content

Commit 0e228b5

Browse files
committed
acl_hash: fix signedness conversion warnings for internal tail buffer
The signedness of `char` is platform-dependent, e.g., signed on the platforms supported by the runtime, which causes the following integer conversion warnings. `char` is appropriate for a buffer of characters, whereas `unsigned char` is appropriate for a buffer of arbitrary bytes. The signedness change only affects the internal implementation, i.e. the internal tail buffer and the internal functions using that buffer. The unit tests provide sufficient coverage to ensure correctness. [2/14] Building C object lib/acl_hash/CMakeFiles/acl_hash.dir/src/acl_hash.c.o ../lib/acl_hash/src/acl_hash.c: In function ‘l_close’: ../lib/acl_hash/src/acl_hash.c:189:14: warning: conversion to ‘char’ alters ‘int’ constant value [-Wconversion] extra[0] = 0x80; // append '1' bit. Assume big endian even within byte. ^~~~ ../lib/acl_hash/src/acl_hash.c:199:20: warning: conversion to ‘char’ from ‘uint64_t {aka long unsigned int}’ may alter its value [-Wconversion] extra[7 - i] = user_bits & 0xff; ^~~~~~~~~
1 parent 91d9f2a commit 0e228b5

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

lib/acl_hash/include/acl_hash/acl_hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ typedef struct {
5555
uint32_t h0, h1, h2, h3, h4;
5656
// The (len % 64) most recent bytes to be added.
5757
// tail[0] is the earliest data byte, and so on.
58-
char tail[64];
58+
unsigned char tail[64];
5959
} acl_hash_sha1_context_t;
6060

6161
typedef struct {

lib/acl_hash/src/acl_hash.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
//////////////////////////////
1919
// Local functions prototypes
2020

21-
static void l_accum(acl_hash_sha1_context_t *c, const char *buf, size_t len);
22-
static void l_accum_block(acl_hash_sha1_context_t *c, const char *buf);
21+
static void l_accum(acl_hash_sha1_context_t *c, const unsigned char *buf,
22+
size_t len);
23+
static void l_accum_block(acl_hash_sha1_context_t *c, const unsigned char *buf);
2324
static void l_close(acl_hash_sha1_context_t *c, char *digest_buf);
2425
static uint32_t l_leftrotate(uint32_t v, unsigned bits);
25-
static uint32_t l_read_bigendian_i32(const char *buf);
26+
static uint32_t l_read_bigendian_i32(const unsigned char *buf);
2627
static void l_write_bigendian_hex_i32(uint32_t v, char *buf);
2728

2829
//////////////////////////////
@@ -53,7 +54,7 @@ int acl_hash_add(acl_hash_context_t *ctx, const void *buf, size_t len) {
5354
return 0;
5455
}
5556

56-
l_accum(&(ctx->alg.sha1), (char *)buf, len);
57+
l_accum(&(ctx->alg.sha1), (unsigned char *)buf, len);
5758

5859
return 1;
5960
}
@@ -87,7 +88,8 @@ int acl_hash_hexdigest(acl_hash_context_t *ctx, char *digest_buf,
8788
//////////////////////////////
8889
// Local functions
8990

90-
static void l_accum(acl_hash_sha1_context_t *c, const char *buf, size_t len) {
91+
static void l_accum(acl_hash_sha1_context_t *c, const unsigned char *buf,
92+
size_t len) {
9193
size_t buf_idx = 0;
9294
size_t tail_idx = c->len & 63;
9395

@@ -127,7 +129,8 @@ static void l_accum(acl_hash_sha1_context_t *c, const char *buf, size_t len) {
127129
}
128130
}
129131

130-
static void l_accum_block(acl_hash_sha1_context_t *ctx, const char *buf) {
132+
static void l_accum_block(acl_hash_sha1_context_t *ctx,
133+
const unsigned char *buf) {
131134
unsigned i;
132135
uint32_t w[80];
133136
uint32_t a = ctx->h0;
@@ -180,7 +183,7 @@ static void l_accum_block(acl_hash_sha1_context_t *ctx, const char *buf) {
180183
}
181184

182185
static void l_close(acl_hash_sha1_context_t *c, char *digest_buf) {
183-
char extra[64];
186+
unsigned char extra[64];
184187
unsigned i;
185188
unsigned raw_tail_len = (1 /* 1-byte */ + 8 /* length field */ + c->len) & 63;
186189
unsigned num_zero_fill_bytes = raw_tail_len > 0 ? (64 - raw_tail_len) : 0;
@@ -217,12 +220,12 @@ static uint32_t l_leftrotate(uint32_t v, unsigned bits) {
217220
return hi | lo;
218221
}
219222

220-
static uint32_t l_read_bigendian_i32(const char *buf) {
223+
static uint32_t l_read_bigendian_i32(const unsigned char *buf) {
221224
// Need to cast to uchar so we don't sign extend when widening out to 32 bits.
222-
uint32_t byte3 = (unsigned char)buf[0];
223-
uint32_t byte2 = (unsigned char)buf[1];
224-
uint32_t byte1 = (unsigned char)buf[2];
225-
uint32_t byte0 = (unsigned char)buf[3];
225+
uint32_t byte3 = buf[0];
226+
uint32_t byte2 = buf[1];
227+
uint32_t byte1 = buf[2];
228+
uint32_t byte0 = buf[3];
226229
uint32_t result = (byte3 << 24) | (byte2 << 16) | (byte1 << 8) | (byte0);
227230
return result;
228231
}

0 commit comments

Comments
 (0)