Buffer overflows, undefined and implementation-defined behavior, integer overflow, sequence points, aliasing, and debugging tools such as gdb and valgrind.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- What is undefined behavior in C?
Show answer
Program behavior that is not defined by the C standard; the program may crash, produce incorrect output, or behave in any way whatsoever.
- How can undefined behavior manifest at runtime?
Show answer
The program may crash, produce wrong results, or appear to work correctly. The standard does not constrain the outcome.
- What is implementation-defined behavior in C?
Show answer
Behavior not specified by the standard, but a conforming compiler must document how it handles the situation.
- Is a C compiler required to warn about undefined behavior?
Show answer
No. The compiler may silently permit undefined behavior. Avoiding it is the programmer's responsibility.
- Name three common sources of undefined behavior in C.
Show answer
Buffer overflow, integer overflow, use-after-free, null pointer dereference, uninitialized variables, accessing out-of-bounds memory, or violating the strict aliasing rule.
- What is a buffer overflow?
Show answer
Writing data beyond the boundary of an allocated buffer, corrupting adjacent memory.
- Why is the gets() function dangerous?
Show answer
It reads input into a buffer without size checking, making buffer overflow inevitable when input exceeds the buffer size.
- What is the vulnerability of strcpy()?
Show answer
It copies a string without checking whether the destination buffer is large enough, allowing buffer overflow.
- What is an off-by-one error?
Show answer
An error in loop bounds or array indexing that causes access to one element beyond the intended range, often from confusion about inclusive vs exclusive bounds.
- Can a stack buffer overflow corrupt heap memory?
Show answer
No. Stack and heap are separate regions. A stack overflow corrupts adjacent stack memory such as return addresses or saved registers.
- What happens when a signed integer overflows in C?
Show answer
Undefined behavior. The result may wrap around, saturate, trap, or behave unpredictably depending on the compiler and platform.
- How does unsigned integer overflow differ from signed?
Show answer
Unsigned overflow wraps modulo 2^n and is well-defined. Signed overflow is undefined behavior.
- How can you detect integer overflow before it occurs?
Show answer
Check operand bounds before the operation. For addition, verify that a <= INT_MAX - b before computing a + b.
- What is integer underflow?
Show answer
The result of an operation is smaller than the minimum representable value. For unsigned types it wraps; for signed types it is undefined.
- Name one defense against integer overflow.
Show answer
Check operands before the operation, use wider types for intermediate results, or use safe arithmetic libraries.