zoukankan      html  css  js  c++  java
  • C 缓冲区过读 if (index >= 0 && index < len)

    C 缓冲区过读 if (index >= 0 && index < len)   

     CWE - CWE-126: Buffer Over-read (3.2) http://cwe.mitre.org/data/definitions/126.html

    CWE - CWE-125: Out-of-bounds Read (3.2) http://cwe.mitre.org/data/definitions/125.html

    #include <stdio.h>
    
    int main () {
    
        int n[ 10 ]; /* n is an array of 10 integers */
        int i,j;
    
        /* initialize elements of array n to 0 */
        for ( i = 0; i < 10; i++ ) {
            n[ i ] = i + 100; /* set element at location i to i + 100 */
        }
    
        /* output each array element's value */
        for (j = 0; j < 10; j++ ) {
            printf("Element[%d] = %d
    ", j, n[j] );
        }
        j=-2;
        printf("Element[%d] = %d
    ", j, n[j] );
        n[j]=-20;
        printf("Element[%d] = %d
    ", j, n[j] );
    
        j=30;
        printf("Element[%d] = %d
    ", j, n[j] );
        n[j]=-300;
        printf("Element[%d] = %d
    ", j, n[j] );
    
        return 0;
    }

    Element[0] = 100
    Element[1] = 101
    Element[2] = 102
    Element[3] = 103
    Element[4] = 104
    Element[5] = 105
    Element[6] = 106
    Element[7] = 107
    Element[8] = 108
    Element[9] = 109
    Element[-2] = -2
    Element[-20] = 2130567168
    Element[30] = 1
    Element[30] = -300
    请按任意键继续. . .

    TODO 内存泄露和指定下标值的解释

  • 相关阅读:
    open_basedir restriction in effect的错误及其解决办法
    SNMP-网络管理协议
    安装cacti监控系统
    并发时-修改Linux系统下的最大文件描述符限制
    js new date()说明
    阿里云ECS环境部署 centos 6.5
    sysbench
    http_load
    LeetCode: Spiral Matrix
    LeetCode:Length of Last Word
  • 原文地址:https://www.cnblogs.com/rsapaper/p/10477348.html
Copyright © 2011-2022 走看看