1

私はスプリントを学び、よりよく理解しようとしていますが、このコードから得られるエラーについて疑問に思っています:

#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戻り値を捨てるのではなく、割り当てる正しい解決策はありますか(警告を取り除きます)?

4

1 に答える 1