Multiple choice technology security

Which statement creates a buffer over flow? (Line numbers are marked using comments /* */)

#include   
#include   
#include   
int main (int argc, char *argv[])  {   
    int i=0,j=1;   
    char ipstring[80];   
    for (;i<=3;i++){    
        cout<<"\n entering a new character\n";    
        j=getchar();/*1*/     
        cout<<”enter a string”;    
        gets(ipstring);/*2*/    
        cout<

  1. 1

  2. 2

  3. Both

  4. None

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Line 2 (gets(ipstring)) creates a buffer overflow because gets() reads input until newline without checking buffer size. Since ipstring is only 80 characters, any input longer than 79 characters will overflow the buffer, corrupting memory. Line 1 (getchar()) is safe as it reads a single character. The gets() function is inherently unsafe and has been deprecated precisely because it cannot prevent buffer overflows - it doesn't take a buffer size parameter.

AI explanation

gets(ipstring) reads an unbounded line of input directly into the fixed 80-byte ipstring array with no length checking, so any input longer than 79 characters overwrites adjacent stack memory — the textbook buffer overflow. getchar() on line 1 only reads a single character into an int, which cannot overflow anything. This is why gets() was eventually removed from the C standard library in favor of bounded alternatives like fgets().