Quiz di C: Buffer Overflows & Bounds

C does not check array bounds, so one wrong index can corrupt memory or open a security hole. This quiz shows realistic snippets and asks you to spot off-by-one errors, unsafe string calls and sneaky signed/unsigned comparisons, and pick the safe fix.

Difficile 10 domande ~8 minuti Superato con il 80% C17

Scegli una modalità

Stai giocando come ospite: accedi per salvare XP e serie.

Domanda di esempio

Domanda 1 di 10

What is the bug in this code?

#include <stdio.h>

int main(void) {
    int a[5];
    for (int i = 0; i <= 5; i++) {
        a[i] = i * i;
    }
    printf("%d\n", a[4]);
    return 0;
}
Mostra la risposta

D. i <= 5 writes to a[5], one element past the end A 5-element array has indexes 0 to 4, so the last iteration writes a[5], outside the array. The fix is i < 5; the <= off-by-one is one of the most common overflow causes.

Cosa copre questo quiz

  • Off-by-one loops and the missing terminator byte
  • What undefined behaviour means for an out-of-bounds write
  • Unsafe calls: gets, strcpy without a size check
  • Bounded alternatives: fgets, snprintf, explicit checks
  • Finding bugs with -fsanitize=address

Domande frequenti

Serve un account?

No. Chiunque può giocare. Accedi per salvare XP, serie, badge e cronologia.

Posso rifarlo?

Sì. Il tuo miglior punteggio resta salvato, e domande e risposte vengono mescolate ogni volta.