need to prevent buffer overflows


Results 1 to 2 of 2

Thread: need to prevent buffer overflows

  1. #1
    Join Date
    Oct 2003
    Posts
    49

    need to prevent buffer overflows

    I am new to C coming from C++ where the strings are easy and memory concerns are just where I parked my car.(Okay we do have new and delete and I check my vectors)
    but all this character array stuff. anyway how do I check my buffer in this code to prevent overflow?

    void do_text(char * path,int argi,char * argt[])
    {
    int out_fd, n_chars,i;
    char buf[BUFFERSIZE];

    if ( (out_fd = open(path,O_RDWR|O_APPEND)) == -1 )
    {
    oops("Cannot open logmesg\n","");

    }

    strcpy(buf,argt[1]);
    strcat(buf," ");
    for (i = 2;i < argi;i++) {
    strcat(buf,argt[i]);
    strcat(buf," ");
    }
    n_chars = strlen(buf);
    if ( write( out_fd, buf, n_chars ) != n_chars)
    perror(path);

    if (close(out_fd) == -1 )
    perror("Error closing files\n");


    }

    I'm just reading the command line and writing it to the file

  2. #2
    Join Date
    Jul 2003
    Posts
    28
    man strncpy, man strncat. But here it looks like you can just check with strlen to make sure argt[1] isn't going to overflow your array of length BUFFERSIZE.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •