【发布时间】:2021-07-03 11:38:21
【问题描述】:
在 64 位系统上,当使用模运算符和 size_t 类型时,我从 clang 分析器收到以下警告:
htable.c:38:62: warning: The result of the ' ' expression is undefined [clang-analyzer-core.UndefinedBinaryOperatorResult]
return ht->num_buckets > 0 ? (ht->hash_fn(key, ht->seed) % ht->num_buckets)
^
hash函数的结果有size_t,桶数也有size_t。最终这转化为无符号长。 AFAIK 结果不能为负,我检查了桶数是否为零。
哈希函数可能会溢出,但由于它用于链接哈希表,因此不会导致问题(如果我没记错的话)。
这里有什么问题? 这是误报吗?
出现警告的函数:
static size_t
htable_bucket_idx(htable_t* ht, void* key)
{
if (!ht || !key) {
printf("htable - bucket_idx: Invalid Argument!\n");
exit(-1);
}
return ht->num_buckets > 0 ? (ht->hash_fn(key, ht->seed) % ht->num_buckets)
: ht->hash_fn(key, ht->seed);
}
哈希函数是 FNV 哈希函数的粗略简化版本:
size_t
fnv_hash_ul(const void* in, unsigned int seed)
{
size_t h = seed;
const unsigned int prime = 0xFDCFB7;
unsigned long ul = *((unsigned long*) in);
return (h ^ ul) * prime;
}
htable_t的定义(为简洁起见省略其他类型,根据要求附加)
typedef size_t (*htable_hash)(const void* in, unsigned int seed);
typedef struct htable
{
htable_hash hash_fn;
htable_keq keq;
htable_cbs_t cbs;
htable_bucket_t* buckets;
size_t num_buckets;
size_t num_used;
unsigned int seed;
} htable_t;
函数的调用:
static int
htable_add_to_bucket(htable_t* ht, void* key, void* value, bool rehash)
{
if (!ht || !key) {
printf("htable - add_to_bucket: Invalid Argument!\n");
exit(-1);
}
size_t idx = htable_bucket_idx(ht, key);
[...]
编译器信息:
clang version 11.1.0
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/10.2.0
Candidate multilib: .;@m64
【问题讨论】:
-
htable_t的定义是什么?您使用的是什么编译器/版本以及如何调用它? -
用所需信息更新了问题。
-
@SomeUsername1
ht->num_buckets未初始化时是否有可能调用该函数? -
@SomeUsername1 我尝试了您的代码序列的一个最小示例,它似乎可以正常工作here 启用
clang-tidy -
@Zoso:original code 确实会出现警告。这更奇怪,因为其余的代码不应该是相关的。
标签: c clang clang-static-analyzer clang-tidy