Handling arguments in a safe way.

Tags:

Follwing codes copies argv[1] into a buffer whose size is predefined.

(1) C version

#include
#include

#define MAX_SIZE 100

int main(int argc, char **argv)
{
char buffer[MAX_SIZE]={0};
int check_len= (strlen(argv[1]));

if( check_len < MAX_SIZE){ strncpy(buffer, argv[1], check_len); buffer[check_len]=‘\0′; } else{ strncpy(buffer, argv[1], MAX_SIZE-1); buffer[MAX_SIZE]=‘\0′; } return 0; } [/code] This code was written by 전혜원. Original post can be found at http://securityproof.net. (2) C++ version

#include
#include

using namespace std;

#define BUFFER_SIZE 100

int main(int argc, char **argv)
{
vector args(argv, argv + argc);
string buffer;

buffer = argv[1];

cout << buffer << endl; return EXIT_SUCCESS; } [/code] This code is written by me, and this is a very typical way of handling arguments. Compare this with the C counterpart shown in item (1). You are welcomed to exploit codes shown above, if possible.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *