The SAMATE Project Department of Homeland Security
Downloads:  Selected

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

Test Case IDDeprecated1320
Bad / GoodBadBad test case
AuthorN/A
Associated test caseN/A
Contributorsindelar
LanguageC
Type of test caseSource Code
Input stringN/A
Expected OutputN/A
InstructionsN/A
Submission date2006-01-27
DescriptioniconOff-by-one error on bounds checking. PLOVER: NUM.OBO, BUFF.OVER
Filename

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

See the comments

Comment #1 :: Code has other errors
Line 22 concatenates to the environment store, through str, instead of to the local buffer, buf. This case was replaced by case 2082
Posted by Paul E. Black :: 2009-04-03 14:57:06

>./strcat-bad2.c
  1. /*
  2.   PLOVER: NUM.OBO, BUFF.OVER
  3. */
  4.  
  5. /*
  6.         Off-by-one error
  7. */
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #define MAXSIZE 40
  14.  
  15. void test(char *str, char *str2){
  16.         char buf[MAXSIZE];
  17.         if(strlen(str) < MAXSIZE)
  18.                 strcpy(buf, str);
  19.         else
  20.                 return;
  21.         if(strlen(str) + strlen(str2) <= MAXSIZE)       
  22.                 strcat(str, str2);
  23.         else
  24.           return;
  25.         printf("results: %s\n", str);
  26. }
  27.  
  28. int main(int argc, char **argv){
  29.         char *userstr;
  30.         char *userstr2;
  31.         if(argc > 2){
  32.                 userstr = argv[1];
  33.                 userstr2 = argv[2];
  34.                 test(userstr,userstr2);
  35.         }
  36.         return 0;
  37. }
  38.