From 4a89027fbbf50092b99e51fb8970b5ea84dc06ee Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Mon, 5 Oct 2026 15:07:20 +0800 Subject: [PATCH] Use bounded unsigned shifts for bit masks --- common_func.c | 2 +- hash_check.c | 6 +++--- librhash/hex.c | 2 +- librhash/rhash.c | 7 +++++-- output.c | 2 +- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/common_func.c b/common_func.c index 04b666ed..28990888 100644 --- a/common_func.c +++ b/common_func.c @@ -193,7 +193,7 @@ char* str_replace_n(const char* src, size_t start_pos, size_t end_pos, const cha int is_binary_string(const char* str) { for (; *str; str++) { - if (((unsigned char)*str) < 32 && ((1 << (unsigned char)*str) & ~0x2600)) { + if (((unsigned char)*str) < 32 && ((1u << (unsigned char)*str) & ~0x2600u)) { return 1; } } diff --git a/hash_check.c b/hash_check.c index 8b13dc1d..38de4920 100644 --- a/hash_check.c +++ b/hash_check.c @@ -248,11 +248,11 @@ static int detect_hash_type(char** ptr, char* end, int* p_len) */ static int is_acceptable_bit_length(int length) { - if ((length & 31) == 0 && length <= 512) + if (length > 0 && (length & 31) == 0 && length <= 512) { int pow = get_ctz(length >> 5); int code = ((length >> (pow + 6)) << 3) | pow; - return (code < 32 && ((1 << code) & 0x101061d)); + return (code < 32 && ((1u << code) & 0x101061du)); } return 0; } @@ -347,7 +347,7 @@ static unsigned bsd_hash_name_to_id(const char* name, size_t length, enum HashNa return (length == strlen(hash_info_table[i].name) ? hash_mask : 0); } /* look for the hash_id in the hash_mask */ - for (hash_id = 1 << i; hash_id && hash_id <= hash_mask; i++, hash_id <<= 1) + for (hash_id = 1u << i; hash_id && hash_id <= hash_mask; i++, hash_id <<= 1) { const char* a; const char* b; diff --git a/librhash/hex.c b/librhash/hex.c index b0119045..ebc793fa 100644 --- a/librhash/hex.c +++ b/librhash/hex.c @@ -134,7 +134,7 @@ size_t rhash_base64_url_encoded_helper(char* dst, const unsigned char* src, size /* RFC 3986: safe url characters are ascii alpha-numeric and "-._~", other characters should be percent-encoded */ static unsigned url_safe_char_mask[4] = { 0, 0x03ff6000, 0x87fffffe, 0x47fffffe }; -#define IS_URL_GOOD_CHAR(c) ((unsigned)(c) < 128 && (url_safe_char_mask[c >> 5] & (1 << (c & 31)))) +#define IS_URL_GOOD_CHAR(c) ((unsigned)(c) < 128 && (url_safe_char_mask[c >> 5] & (1u << (c & 31)))) /** * URL-encode specified binary string. diff --git a/librhash/rhash.c b/librhash/rhash.c index 56c4aee5..87058f78 100644 --- a/librhash/rhash.c +++ b/librhash/rhash.c @@ -1057,8 +1057,11 @@ static unsigned ids_array_to_hash_bitmask(size_t count, unsigned* data) bitmask |= data[i]; else if (data[i] == RHASH_ALL_HASHES) bitmask |= RHASH_LOW_HASHES_MASK; - else - bitmask |= 1 << GET_EXTENDED_HASH_ID_INDEX(data[i]); + else { + unsigned index = GET_EXTENDED_HASH_ID_INDEX(data[i]); + if (index < RHASH_HASH_COUNT) + bitmask |= 1u << index; + } } return bitmask; } diff --git a/output.c b/output.c index 813a97de..fe4c5a41 100644 --- a/output.c +++ b/output.c @@ -173,7 +173,7 @@ int fprintf_file_t(FILE* out, const char* format, struct file_t* file, unsigned /* RFC 3986: safe url characters are ascii alpha-numeric and "-._~", other characters should be percent-encoded */ static unsigned url_safe_char_mask[4] = { 0, 0x03ff6000, 0x87fffffe, 0x47fffffe }; -#define IS_URL_GOOD_CHAR(c) ((unsigned)(c) < 128 && (url_safe_char_mask[c >> 5] & (1 << (c & 31)))) +#define IS_URL_GOOD_CHAR(c) ((unsigned)(c) < 128 && (url_safe_char_mask[c >> 5] & (1u << (c & 31)))) /** * Print to a stram an url-encoded representation of the given string.