Ubuntu下面的C語(yǔ)言代碼檢查工具Splint

字號(hào):

看一下下面的代碼(當(dāng)然包括錯(cuò)誤,以檢驗(yàn)splint的功能):
    #include
    int main(int argc,char* argv[]){
    int a=100; /*沒(méi)有使用的變量*/
    int b[8];
    printf("Hello c\n");
    b[9]=100; /*明顯數(shù)組越界 */
    /* 用到了兩個(gè)為聲明的變量c和d/
    c=100;
    d=10;
    return 0;
    }
    現(xiàn)在可以用splint來(lái)檢查一下,為了檢驗(yàn)是否可以檢測(cè)到數(shù)組越界,使用+bounds選項(xiàng)。
    splint hi.c +bounds
    輸出結(jié)果:
    hi.c: (in function main)
    hi.c:9:2: Unrecognized identifier: c
    Identifier used in code has not been declared. (Use -unrecog to inhibit
    warning)
    hi.c:10:2: Unrecognized identifier: d
    hi.c:4:6: Variable a declared but not used
    A variable is declared but never used. Use /*@unused@*/ in front of
    declaration to suppress message. (Use -varuse to inhibit warning)
    hi.c:7:2: Likely out-of-bounds store:
    b[9]
    Unable to resolve constraint:
    requires 7 >= 9
    needed to satisfy precondition:
    requires maxSet(b @ hi.c:7:2) >= 9
    A memory write may write to an address beyond the allocated buffer. (Use
    -likely-boundswrite to inhibit warning)
    hi.c:3:14: Parameter argc not used
    A function parameter is not used in the body of the function. If the argument
    is needed for type compatibility or future plans, use /*@unused@*/ in the
    argument declaration. (Use -paramuse to inhibit warning)
    hi.c:3:25: Parameter argv not used
    Finished checking --- 6 code warnings
    現(xiàn)在詳細(xì)看一下結(jié)果:
    檢查結(jié)果1:
    hi.c:9:2: Unrecognized identifier: c
    Identifier used in code has not been declared. (Use -unrecog to inhibit
    warning)
    hi.c:10:2: Unrecognized identifier: d
    hi.c:4:6: Variable a declared but not used
    A variable is declared but never used. Use /*@unused@*/ in front of
    declaration to suppress message. (Use -varuse to inhibit warning)
    這些應(yīng)該是splint檢測(cè)到變量c和d沒(méi)有聲明。
    檢查結(jié)果2:
    hi.c:7:2: Likely out-of-bounds store:
    b[9]
    Unable to resolve constraint:
    requires 7 >= 9
    needed to satisfy precondition:
    requires maxSet(b @ hi.c:7:2) >= 9
    A memory write may write to an address beyond the allocated buffer. (Use
    -likely-boundswrite to inhibit warning)
    這些是檢查存在數(shù)組越界,因?yàn)榘蒪[8]的數(shù)組序號(hào)應(yīng)該是7,而不是9,所以出現(xiàn)requires 7 >= 9;
    檢查結(jié)果3:
    hi.c:3:14: Parameter argc not used
    A function parameter is not used in the body of the function. If the argument
    is needed for type compatibility or future plans, use /*@unused@*/ in the
    argument declaration. (Use -paramuse to inhibit warning)
    hi.c:3:25: Parameter argv not used
    這些表明argc和argv變量聲明了,考試,大提示沒(méi)有使用。這個(gè)不是什么問(wèn)題。
    如果小心使用splint,應(yīng)該對(duì)于c語(yǔ)言的程序編寫有非常大的輔助作用!