is this program safe from buffer overflow problems

  1. #1
    Olpah's Object Reputation: 323
    postrook's Avatar
    Join Date
    Jun 2012
    Location
    killjoy
    Posts
    2,788
    Rep Power
    10

    Default is this program safe from buffer overflow problems

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char t;
        char a[3];
        char b='z';
        int n=0;
        while(scanf("%c",&t) && n < 2) {
            a[n] = t;
            n++;
            a[n]='\0';
        }
        printf("%s , %c",a, b);
        return 0;
    }
    to test it, i copypasted 4194304 'a' characters onto the command line.

    output was:

    aa , z


    does that mean this is a safe method of getting user input? anything else i should be aware of?
    I hate TALKING. to PEOPLE. about THINGS.

  2. #2
    The Undead Reputation: 470
    Z0MBiE's Avatar
    Join Date
    Dec 2007
    Location
    [REDACTED]
    Posts
    5,416
    Rep Power
    18

    Default

    seems fine to me, although it's been years and years since I tried this stuff. could be something I'm forgetting.

    anything else i should be aware of?
    as much as I hate to admit it, using C++ is safer.


Posting Permissions

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