The SAMATE Project Department of Homeland Security
Downloads:  Selected

Back to the previous page...Back to the previous page

Test Case IDCandidate4
Bad / GoodBadBad test case
AuthorN/A
Associated test caseN/A
ContributorSecureSoftware
LanguageC
Type of test caseSource Code
Input stringN/A
Expected OutputThe last character in shortString is: l 6c
InstructionsN/A
Submission date2005-10-21
DescriptioniconMiscalculated null termination occurs when the placement of a null character at the end of a buffer of characters (or string) is misplaced or omitted. (from TCCLASP-5_2_14_9)
Filename
Flaw
  • (?) CWE-170: Improper Null Termination at line 6

There is 1 comment :: Submit a comment :: RSS

>./Miscalculated_null_termination.c
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main() {
  4. char longString[] = "Cellular bananular phone";
  5. char shortString[16];
  6. strncpy(shortString, longString, 16);
  7. printf("The last character in shortString is: %c %1$x\n",
  8. shortString[15]);
  9. return (0);
  10.  
  11. /*
  12. The above code gives the following output:
  13. The last character in shortString is: l 6c
  14. So, the shortString array does not end in a NULL character, even though the
  15. “safe” string function strncpy() was used.
  16. */}
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.