我正在尝试学习和更好地理解splint,我想知道我从这段代码中得到的一个错误:
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
/*@null@*/ /*@only@*/ char *dupStr(const char *str) {
char *copy;
size_t len;
len = strlen(str) + 1U;
if (!(copy = malloc(len * sizeof *str))) {
return NULL;
}
(void) strncpy(copy, str, len);
return copy;
}错误是:
Splint 3.1.2 --- 26 Feb 2013
test.c: (in function dupStr)
test.c:13:9: New fresh storage copy (type void) cast to void (not released):
(void)strncpy(copy, str, len)
A memory leak has been detected. Storage allocated locally is not released
before the last reference to it is lost. (Use -mustfreefresh to inhibit
warning)
Finished checking --- 1 code warning正确的解决方案是将返回值分配给copy,而不是丢弃它(它消除了警告)吗?
发布于 2014-01-26 23:49:52
你不想忽略strncpy的返回值,这就是splint抱怨的原因。你想要这样的东西:
if (strncpy(copy, str, len) == NULL)
return NULL;https://stackoverflow.com/questions/19464573
复制相似问题