The SAMATE Project Department of Homeland Security
Downloads:  Selected

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

Test Case IDCandidate2081
Bad / GoodBadBad test case
AuthorN/A
Associated test caseN/A
ContributorPaul E. Black
LanguageC
Type of test caseSource Code
Input string./a.out a234567890 b234567890XY
Expected Output final: XYa234567890b234567890XY>
InstructionsN/A
Submission date2009-04-03
DescriptioniconNo bounds checking on buffer during strcat(). PLOVER: BUFF.OVER This replaces case 1319
Filename
Flaw
  • (?) CWE-121: Stack-based Buffer Overflow at line 23

There is no comments :: Submit a comment :: RSS

>./strcat-bad1.c
  1. /*
  2.   PLOVER: BUFF.OVER
  3. */
  4.  
  5. /*
  6.         No bounds checking of strcat()
  7.     input: ./a.out a234567890 b234567890XY
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #define MAXSIZE 20
  14.  
  15. void test(char *str, char *str2){
  16.     char pre[2] = "<";
  17.     char buf[MAXSIZE] = "";
  18.     char post[2] = ">";
  19.     if(strlen(str) < MAXSIZE)
  20.         strcpy(buf, str);
  21.     printf(" strcpy: %s%s%s\n", pre, buf, post);
  22.     strcat(buf, str2); // CWE-121
  23.     printf("results: %s%s%s\n", pre, buf, post);
  24. }
  25.  
  26. int main(int argc, char **argv){
  27.     char *userstr;
  28.     char *userstr2;
  29.     if(argc > 2){
  30.         userstr = argv[1];
  31.         userstr2 = argv[2];
  32.         test(userstr,userstr2);
  33.     }
  34.     printf("done\n");
  35.     return 0;
  36. }
  37.