2011-11-19 24 views
1

我必須確保作爲參數傳遞的字符串不會導致溢出。我被用strncpy這樣做,但結束「\ 0」,分配的內存適量等等給了我一些麻煩......請確保一個字符串作爲參數傳遞不會導致溢出

我的解決辦法是這樣的:

l = strlen(argv[optind]); 
if(l<MAX_LENGTH) { 
    msg = malloc((l+1) * sizeof(char)); 
    msg = strcpy(msg, argv[optind]); 
} else { 
    msg = malloc((MAX_LENGTH+1) * sizeof(char)); 
    msg = strncpy(msg, argv[optind], MAX_LENGTH); 
    msg[MAX_LENGTH+1] = '\0'; 
} 

它作品,但我想知道它是否真的正確,如果有更緊湊的解決方案?

+3

我認爲這是正確的編碼上述方式。但是你也可以直接將argv [optind](通過參數或指針)傳遞給你的內部例程,而不用擔心這種限制。 –

+1

您可能想要考慮一下這樣一個事實,即您的平臺很可能已經對參數大小進行了限制,因此您可以隨時使用「l + 1」。 –

+0

你應該使用strnlen – SoapBox

回答

2

我認爲這是最簡單的你可以得到:

size_t l; 
char* msg; 
... 
l = strlen(argv[optind]); 
if (l > MAX_LENGTH) l = MAX_LENGTH; 

msg = malloc(l + 1); 
if (msg == NULL) /* handle the error as appropriate*/; 

memcpy(msg, argv[optind], l); 
msg[l] = '\0'; 
+0

GNU函數strnlen(可以輕鬆地重新實現的擴展)很有趣,可以避免strlen的另一個潛在問題(考慮execX系列函數) – ShinTakezou

1

,也許可以與取代所有這些代碼:

msg = strdup(argv[optind]); 

strdup(3)

The strdup() function returns a pointer to a new string which 
    is a duplicate of the string s. Memory for the new string is 
    obtained with malloc(3), and can be freed with free(3). 

    The strndup() function is similar, but only copies at most n 
    characters. If s is longer than n, only n characters are 
    copied, and a terminating null byte ('\0') is added. 

更新

CONFORMING TO 
    strdup() conforms to SVr4, 4.3BSD, POSIX.1-2001. strndup(), 
    strdupa(), and strndupa() are GNU extensions. 
+0

但是,'strdup'不在標準庫中...... –

+1

這具有語義上的區別,它不會對結果分配進行限制。 –

+0

@Oli:很好。我很習慣用我的POSIX盲目思考。 :) – sarnold

1
l = strlen(argv[optind]); 
if (l < MAX_LENGTH) { 
    msg = malloc(l+1); 
    if (msg) strcpy(msg, argv[optind]); 
} else { 
    msg = malloc(MAX_LENGTH+1); 
    if (msg) { 
#if 1 
     memcpy(msg, argv[optind], MAX_LENGTH); 
#else 
     strncpy(msg, argv[optind], MAX_LENGTH); 
#endif 
     msg[MAX_LENGTH] = '\0'; 
    } 
} 
+0

這有什麼好處?我認爲它不會提高可讀性,也不會更緊湊。 – mort

+2

區別在最後一行。 OP使用MAX_LENGTH + 1作爲索引(這是錯誤的)。另外,我檢查malloc()返回NULL。 – wildplasser

+0

請不要更新您的帖子以隱藏您的錯誤。這會使未來的觀衆對這些反應毫無用處。 – wildplasser

相關問題