我最近开始学习C,我正在以C为主题.我正在玩循环,我遇到了一些我不知道如何解释的奇怪行为.
#include <stdio.h>
int main()
{
int array[10],i;
for (i = 0; i <=10 ; i++)
{
array[i]=0; /*code should never terminate*/
printf("test \n");
}
printf("%d \n", sizeof(array)/sizeof(int));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我的运行Ubuntu 14.04的笔记本电脑上,此代码不会中断.它运行完成.在我学校的运行CentOS 6.6的计算机上,它运行良好.在Windows 8.1上,循环永远不会终止.
更奇怪的是,当我将for循环条件编辑为:时i <= 11,代码只会在运行Ubuntu的笔记本电脑上终止.它永远不会在CentOS和Windows中终止.
任何人都可以解释内存中发生的事情以及运行相同代码的不同操作系统为什么会产生不同的结果?
编辑:我知道for循环超出范围.我是故意这样做的.我无法弄清楚不同操作系统和计算机之间的行为有何不同.
当我尝试编译使用gets()GCC函数的C代码时,
我明白了
警告:
(.text + 0x34):警告:`gets'函数很危险,不应该使用.
我记得这与堆栈保护和安全性有关,但我不确定为什么?
有人可以帮我删除这个警告并解释为什么会有这样的使用警告gets()?
如果gets()是如此危险,为什么我们不能删除它?
int func(char* str)
{
char buffer[100];
unsigned short len = strlen(str);
if(len >= 100)
{
return (-1);
}
strncpy(buffer,str,strlen(str));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码容易受到缓冲区溢出攻击,我正在试图找出原因.我认为它与len被宣布为一个short而不是一个int,但我不是很确定.
有任何想法吗?
编辑:我添加了示例的源代码.
我遇到了这个例子:
char source[MAX] = "123456789";
char source1[MAX] = "123456789";
char destination[MAX] = "abcdefg";
char destination1[MAX] = "abcdefg";
char *return_string;
int index = 5;
/* This is how strcpy works */
printf("destination is originally = '%s'\n", destination);
return_string = strcpy(destination, source);
printf("after strcpy, dest becomes '%s'\n\n", destination);
/* This is how strncpy works */
printf( "destination1 is originally = '%s'\n", destination1 );
return_string = strncpy( destination1, source1, index );
printf( "After strncpy, destination1 becomes '%s'\n", destination1 );
Run Code Online (Sandbox Code Playgroud)
哪个产生了这个输出: …
我正在处理一个需要禁用编译器优化保护才能工作的作业问题.我在ubuntu linux上使用gcc 4.4.1,但无法弄清楚哪些是正确的.我意识到它依赖于架构 - 我的机器运行32位英特尔处理器.
谢谢.
编程中堆栈溢出和缓冲区溢出有什么不同?
我找不到回答这个问题的好消息来源.我知道nop sled是一种用于避免缓冲区溢出攻击中的堆栈随机化的技术,但我无法理解它是如何工作的.
说明这种方法的简单例子是什么?
像128字节的nop雪橇这样的术语是什么意思?
我只是尝试在VS2010上编译几个C++片段并分析IDA Pro上的可执行文件.我注意到的是,他们中的大多数在开始时都有类似的内容(在调用__security_check_cookie之后不久)
xor eax, ebp
和类似的东西
xor ecx, ebp
在底部.为什么会这样?编译器优化已关闭.
c++ assembly reverse-engineering buffer-overflow compiler-optimization
"普通人不想自由.他只是想要安全." - HL Menken
我正在尝试编写非常安全的C.下面我列出了一些我使用的技术,并且询问它们是否像我认为的那样安全.请不要犹豫将我的代码/先入之见撕成碎片.任何能找到最微不足道的漏洞或者教我一个新想法的答案都会受到高度重视.
根据GNU C编程教程 getline:
getline函数将根据需要通过realloc函数自动扩大内存块,因此永远不会缺少空间 - 这是getline如此安全的一个原因.[..]请注意,无论多长时间,getline都可以安全地处理您的输入线.
我假设getline应该在所有输入下防止从流中读取时发生缓冲区溢出.
如果malloc遇到错误,malloc将返回NULL指针.这会带来安全风险,因为仍然可以将指针算法应用于NULL(0x0)指针,因此维基百科推荐
/* Allocate space for an array with ten elements of type int. */
int *ptr = (int*)malloc(10 * sizeof (int));
if (ptr == NULL) {
/* Memory could not be allocated, the program should handle
the error here as appropriate. */
}
Run Code Online (Sandbox Code Playgroud)
当使用sscanf时,我已经养成了将要提取的字符串分配给输入字符串大小的习惯,希望避免出现溢出的可能性.例如:
const char *inputStr = "a01234b4567c";
const …Run Code Online (Sandbox Code Playgroud)