#include <string.h>
void foo (char *bar)
{
char c[12];
strcpy(c, bar); // no bounds checking
}
int main (int argc, char **argv)
{
foo(argv[1]);
return 0;
}
-1
A
回答
-2
2
另一个实际选项:
void foo (char *bar)
{
char c[12];
snprintf(c, sizeof c, "%s", bar);
}
功能strncpy
不保证生成的字符串由NULL character
终止,因此,你可以有一个bad resulting string.
0
检查是否长度该bar
小于length of c
。
代码:
#include <string.h>
void foo (char *bar)
{
char c[12];
if (strlen(bar) < sizeof c) {
printf("avoiding buffer overflow");
strcpy(c, bar);
}
}
int main (int argc, char **argv)
{
foo(argv[1]);
return 0;
}
相关问题
- 1. jquery if statement issue
- 2. IF statement validate.js
- 3. if statement ambiguity
- 4. svg if statement
- 5. iMacro If Else Statement
- 6. Symfony2 {%if statement%}
- 7. If/Else Statement
- 8. MYSQL if statement question
- 9. JavaScript if statement scope
- 10. if statement with datatables
- 11. NSArray和“If-statement”
- 12. Long if statement
- 13. Javascript - if statement error
- 14. IF statement == not working
- 15. if statement and mutate
- 16. If or If-Else Statement Error
- 17. If-Statement for String/Int
- 18. If Else statement in Fullcalendar
- 19. if statement(banking - credit/debit)
- 20. If-Statement Help in PHP
- 21. If-Else statement with JSF
- 22. If-statement with changing url
- 23. Nestled IF(NOT(ISBLANK))Statement?
- 24. if statement not reading variables
- 25. If/Else Statement Not Evaluating
- 26. If/Else Statement Location - Javascript
- 27. PHP MYSQL if null statement
- 28. Google Spreadsheet long IF statement?
- 29. 何时使用Statement over Prepared Statement?
- 30. JavaScript if if select statement from select box
“如何使这个代码利用IF..Statement缓冲区溢出的安全”,没有必要为一个if语句,只需使用['strncpy'(HTTPS:/ /linux.die.net/man/3/strncpy),而不是:-) – George
提示您可能还需要检查参数是否的确存在。 – sjsam
注意,仅仅是避免缓冲区溢出并不能保证你的程序实际上会做正确的事。无论您选择何种策略来防止缓冲区在任何特定情况下溢出,都应该与程序目的和规范保持一致。 –