C 自动边界检查?

C 自动边界检查?,c,arrays,string,overflow,buffer,C,Arrays,String,Overflow,Buffer,我对编程相当陌生,一直在学习C。我试图生成一个简单的缓冲区溢出,并能够作为练习预测结果,但在尝试这样做时,甚至无法创建溢出。我正在使用MinGW,它似乎会自动调整我的数组以适应内容。我正在使用-Wall和-Wextra进行编译,但没有排除任何错误。这里到底发生了什么?为什么不给我一个错误?我的nNum不应该被写下来吗?当我随意给不该碰的地方写信时,难道不应该有什么抱怨吗?谢谢 #include <stdio.h> #include <string.h> /* Arra

我对编程相当陌生,一直在学习C。我试图生成一个简单的缓冲区溢出,并能够作为练习预测结果,但在尝试这样做时,甚至无法创建溢出。我正在使用MinGW,它似乎会自动调整我的数组以适应内容。我正在使用-Wall和-Wextra进行编译,但没有排除任何错误。这里到底发生了什么?为什么不给我一个错误?我的nNum不应该被写下来吗?当我随意给不该碰的地方写信时,难道不应该有什么抱怨吗?谢谢

#include <stdio.h>
#include <string.h>

/*  Array index - something to be sure that you're outside of szArray  */
#define SZ_LOCATION 15

int main(void)
{
    /*  Initialize array and number.  Store 1000 to number, and "hello\0" to array  */
    char szArray[6];
    unsigned short nNum = 1000;
    strcpy(szArray, "hello");

    printf("SZ_LOCATION = %i\n\n", SZ_LOCATION);

    /*  Print current contents of szArray ("hello").  Print the char at the preset index location, and the current (unchanged) value of nNum  */
    printf("szArray = %s\n", szArray);
    printf("szArray[SZ_LOCATION] = %c\n", szArray[SZ_LOCATION]);
    printf("nNum = %d\n\n", nNum);

    /*  Add 3 chars to szArray, to push it over 6 chars.  Re-print all variables  */
    strcat(szArray, "BIG");
    printf("szArray = %s\t(%I64u bytes)\nszArray[7] = %c\nnNum = %d\n\n", szArray, sizeof(szArray), szArray[sizeof(szArray) + 1], nNum);

    /*  Store a random char to the preset location in the array, way out there, and re-print its contents, with the new size of the array  */
    szArray[SZ_LOCATION] = 'h';
    printf("szArray = %s\nszArray[SZ_LOCATION] = %c\nsizeof(szArray) = %I64u\n", szArray, szArray[SZ_LOCATION], sizeof(szArray));

    return 0;
}
#包括
#包括
/*数组索引-确保您在数组之外的东西*/
#定义SZ_位置15
内部主(空)
{
/*初始化数组和数字。将1000存储到数字,将“hello\0”存储到数组*/
字符数组[6];
无符号短nNum=1000;
strcpy(szArray,“hello”);
printf(“SZ_位置=%i\n\n”,SZ_位置);
/*打印szArray(“hello”)的当前内容。打印预设索引位置的字符,以及nNum的当前(不变)值*/
printf(“szArray=%s\n”,szArray);
printf(“szArray[SZ_位置]=%c\n”,szArray[SZ_位置]);
printf(“nNum=%d\n\n”,nNum);
/*向szArray中添加3个字符,使其超过6个字符。重新打印所有变量*/
strcat(szArray,“大”);
printf(“szArray=%s\t(%I64u字节)\nszArray[7]=%c\nnNum=%d\n\n”,szArray,sizeof(szArray),szArray[sizeof(szArray)+1],nNum;
/*将一个随机字符存储到数组中的预设位置,然后以数组的新大小重新打印其内容*/
szArray[SZ_位置]='h';
printf(“szArray=%s\nszArray[SZ_位置]=%c\nsizeof(szArray)=%I64u\n”,szArray,szArray[SZ_位置],sizeof(szArray));
返回0;
}

它不会调整数组以适应内容。由于戏剧性的巧合,超出数组限制的寻址并没有破坏任何重要内容。更具体地说:数组是在堆栈上分配的,您可以写得足够远,不会弄乱临时的
push
/
pop
s,或者编译器决定根本不需要它们,您可以使用未使用的堆栈空间。请在之后或之前触摸一些东西以获得任何效果。

更改此选项

/*  Add 3 chars to szArray, to push it over 6 chars.  Re-print all variables  */
strcat(szArray, "BIG");
printf("szArray = %s\t(%I64u bytes)\nszArray[7] = %c\nnNum = %d\n\n", szArray, sizeof(szArray), szArray[sizeof(szArray) + 1], nNum);
将来


并监视程序的输出。

程序运行良好(我的意思是它给了我一个严重的错误),你想实现什么?我使用
gcc-Wall
进行编译,因为未定义的行为并不意味着您总能看到它;这意味着有一天,在某个地方,当你给一个写着8位数支票的潜在购买伙伴做演示时,你很可能会看到它(他们也会看到,然后会有很多人哭泣、哭泣和咬牙切齿……)。你是在要求一些“未定义”的东西采取“确定”的行动。@WhozCraig在[C]标签的入口处应该有一个非常大的横幅。这个问题每天都以各种各样的形式重复着:)@WhozCraig我明白你在说什么。也许我只是希望C比它更能握住我的手。在阅读了alk的回答之后,我想我可以看到发生了什么。谢谢@user3316425除了编译时检查(它们是非常基本的)之外,C和汇编语言一样保存had。也就是说,它没有。很久以前,人们常说的一句话是“C:汇编语言的所有功能,以及汇编语言的安全性。”我在这一点上犯了错误。当我开始递增szArray并更改其内容时,它也起作用。我想我现在可以看到更多关于这件事的信息。非常感谢。
while (1)
{
  /*  Add 3 chars to szArray, to push it over 6 chars.  Re-print all variables  */
  strcat(szArray, "BIG");
  printf("szArray = %s\t(%I64u bytes)\nszArray[7] = %c\nnNum = %d\n\n", szArray, sizeof(szArray), szArray[sizeof(szArray) + 1], nNum);
}