首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Splint:来自strcpy()的新存储?

Splint:来自strcpy()的新存储?
EN

Stack Overflow用户
提问于 2013-10-19 17:37:43
回答 1查看 151关注 0票数 1

我正在尝试学习和更好地理解splint,我想知道我从这段代码中得到的一个错误:

代码语言:javascript
复制
#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;
}

错误是:

代码语言:javascript
复制
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,而不是丢弃它(它消除了警告)吗?

EN

回答 1

Stack Overflow用户

发布于 2014-01-26 23:49:52

你不想忽略strncpy的返回值,这就是splint抱怨的原因。你想要这样的东西:

代码语言:javascript
复制
if (strncpy(copy, str, len) == NULL)
    return NULL;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19464573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档