cybersecurity · beginner · ~15 min · safe pentest lab

Longest NOP run

Find the longest run of 0x90 (x86 NOP) — the signature of a NOP sled.

Challenge

Classic stack-smashing shellcode is preceded by a NOP sled (a long run of 0x90) so an imprecise jump still lands in it. Implement:

int longest_nop_run(const unsigned char *buf, size_t n);

Return the length of the longest run of consecutive 0x90 bytes.

Input format

buf of n bytes.

Output format

Longest 0x90 run length.

Constraints

n==0 returns 0.

Starter code

#include <stddef.h>
/* Longest run of 0x90 (x86 NOP) bytes in buf. */
int longest_nop_run(const unsigned char *buf, size_t n){ (void)buf;(void)n; return 0; }

Common mistakes

Not resetting the run on a non-NOP byte; missing the run that ends at the buffer's end (handled naturally if you track the max as you go).

Edge cases to handle

Runs at the start/end count; a non-0x90 byte resets the run.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.