2013-10-19 2 views
1

내가 부목을 배우고 더 잘 이해하기 위해 노력하고있어, 나는이 코드에서 얻을 오류에 대해 궁금 해요 :부목 : strcpy()의 새로운 새로운 저장소?

#include <stddef.h> 
#include <stdlib.h> 
#include <string.h> 

/*@[email protected]*/ /*@[email protected]*/ 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에 할당하는 대신 경고를 제거합니다 (경고를 제거함).

+1

나는 당신이 가지고있는 것처럼 (또는 거의 그렇게) 썼을 것이다. 그래서 나는 그 불평이 무엇인지 분명하지 않다. – WhozCraig

+4

분명히'(void) '를 제거하면 해당 경고가 무시됩니다. 흥미로운 도구입니다. – WhozCraig

+2

Hej, 사이드 노트 :'(void) strncpy (copy, str, len); 대신'(void) memcpy (copy, str, len); (이것은 부딪 치기가 아닌 퍼포먼스 아이디어입니다) – chux

답변

0

의 반환 값을 무시하고 싶지 않으므로 splint이 불만입니다. 당신은 다음과 같은 것을 원합니다 :

if (strncpy(copy, str, len) == NULL) 
    return NULL; 
관련 문제