본문 바로가기
Study/Software Security

[소프트웨어보안] Argc/Argv Programming, sscanf

by 8희 2022. 10. 5.

Argc/Argv Programming

#include <stdio.h>

int main (int argc, char *argv[]){
  int count;
  printf ("This program was called with \"%s\".\n",argv[0]);
  if (argc > 1) {
      for (count = 1; count < argc; count++){
	  printf("argv[%d] = %s\n", count, argv[count]);
	}
    }
  else     {
      printf("The command had no other arguments.\n");
    }
  return 0;
}

 


sscanf의 활용

/* sscanf example */
#include <stdio.h>

int main ()
{
  char sentence []=“John is 12 years old";
  char str [10];
  int i;

  sscanf (sentence,"%s is %d",str, &i);
  printf ("%s -> %d\n",str,i); //출력 결과: John -> 12
  
  return 0;
}
/* sscanf example 2*/
#include <stdio.h>

int main ()
{
  char sentence []=“John_Hyung_Kim is 1238179278479183749173 years old";
  char str [10];
  int i;

  sscanf (sentence,"%s is %d",str, &i); //integer overflow 발생 지점
  printf ("%s -> %d\n",str,i); //출력 결과: John_Hyung_Kim -> -1
  
  return 0;
}