Recently I have been studying C expert programming and mentioned the C language code check tool lint under Ubuntu7.10. Using aptitudesearch, we did not find the lint software package, but found the splint tool. Check the following code (including errors to check splint functions): # define deintmain (intargc, char * argv []) {inta = 100; /* unused variables */intb [
Recently I have been studying C expert programming and mentioned the code check tool for lint in Ubuntu 7.10.
You can use aptitude to search without finding the lint software package. Instead, you can find the splint tool.
Check the following code (including errors to verify the splint function ):
# Include
Int main (int argc, char * argv []) {
Int a = 100;/* unused variable */
Int B [8];
Printf ("Hello c \ n ");
B [9] = 100;/* Apparently out-of-bounds array */
/* Two declared variables c and d/are used/
C = 100;
D = 10;
Return 0;
}
Now we can use splint to check whether the array is out of bounds and use the + bounds option.
Splint hi. c + bounds
Output result:
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
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
Argument declaration. (Use-paramuse to inhibit warning)
Hi. c: 3: 25: Parameter argv not used
Finished checking --- 6 code warnings
Now let's take a closer look at the results:
Check Result 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
Declaration to suppress message. (Use-varuse to inhibit warning)
These variables c and d are not declared by splint.
Check Result 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)
These are checks for the existence of array out-of-bounds, because B [8]'s maximum array number should be 7, rather than 9, so the emergence of requires 7> = 9;
Check Result 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
Argument declaration. (Use-paramuse to inhibit warning)
Hi. c: 3: 25: Parameter argv not used
These indicate that the argc and argv variables are declared but not used. This is not a problem.
If you use splint carefully, it should be very helpful for programming in C language!