# 1 · The Negative Packet Header

Welcome. A program has room for 64 bytes, small units of data. An incoming message, or **packet**, starts with a count that says how many bytes to copy. That first part is the **header**. A wrong count could put the rest of the message in space reserved for something else.

Try the rule on paper. A count of 64 fits. A count of 65 does not. What should the program do if the count says minus four? After this lesson, you can decide which counts the program should accept before it copies a single byte.

The example below is written in C, a programming language. Read `len` as the incoming count, `>` as “greater than” and `return` as “give back this answer and stop.” The count has type `int16_t`, a whole number from −32,768 to 32,767. The answer has type `size_t`, an unsigned number used for sizes. It cannot be negative. Subtracting one from zero in this type wraps around to its largest value. This example only plans a copy.

Follow the two possible answers in the code. If the count is greater than 64, the first line marked `return` gives back zero. Otherwise, the program reaches the second one. There, `(uint16_t)` means “convert to a whole number from 0 to 65,535.” A negative count wraps around to the high end of that range.

Predict the output of `buggy_plan_copy(-4)` before you run it. Does the check return `0`, `-4`, or `65532`?

```c
size_t buggy_plan_copy(int16_t len) {
    if (len > 64) {
        return 0;
    }
    return (uint16_t)len;
}
```

To check your prediction, build and run the C example from the `courses/zero-day-lab` folder:

```sh
sh native-lab/verify.sh
./native-lab/packet_gate -4 0 64 65
```

The diagram writes bytes in **hexadecimal** (base 16). Its digits run from 0 to 9, then from a to f. The prefix `0x` marks a number written this way. **Little-endian** means the byte for the smaller place values comes first. In a signed 16-bit count, the leftmost bit (bit 15) gives the sign. A 1 there means the count is negative.

![Wire bytes fc ff decode in little-endian as 0xfffc. The count is -4. The comparison is false, so the program reaches the conversion and returns 65,532. Casting to uint16_t before comparing blocks 65532 and returns 0.](figures/header-wraparound.svg)

## Why `-4` slips past the upper bound

The first line shows the size of the header in bytes. Here `sizeof(frame_hdr_t)` counts four bytes, two for the length and two for a label.

```text
sizeof(frame_hdr_t) = 4 bytes
wire_le  signed_i16 unsigned_u16 buggy_plan fixed_plan
fc ff    -4         65532        65532       0
00 00    0          0            0           0
40 00    64         64           64          64
41 00    65         65           0           0
```

This signed type uses **two's complement**. When bit 15 is set, subtract 65,536 from the unsigned reading to get the signed count. In **little-endian** order, low byte `fc` sits at offset `0` and `ff` sits at offset `1`, forming `0xfffc`.

The **source** is where untrusted input enters, here through the packet header. The **sink** is where the program uses that input, here to plan a memory copy. Follow the count from one to the other. The program decodes `0xfffc` as `int16_t len = -4`. Because `-4` is less than `64`, `if (len > 64)` is false and skips `return 0;`.

## The cast turns `-4` into `65,532`

At the sink, `return (uint16_t)len;` converts `-4` to an **unsigned** 16-bit integer (`uint16_t`), adding `65,536` to yield `65,532`. C defines the result of this conversion. A copy outside the buffer would be **undefined behavior**. C gives no guarantees about what happens, and the program could silently corrupt memory. The check must reject an unsafe count before the copy.

| Wire bytes (LE) | 16-bit hex | `int16_t` view | `uint16_t` view | `buggy_plan` | `fixed_plan` |
| --- | --- | --- | --- | --- | --- |
| `fc ff` | `0xfffc` | `-4` | `65532` | `65532` | `0` |
| `00 00` | `0x0000` | `0` | `0` | `0` | `0` |
| `40 00` | `0x0040` | `64` | `64` | `64` | `64` |
| `41 00` | `0x0041` | `65` | `65` | `0` | `0` |

Run the Node.js command-line program on `-1` and `-32768` to see how every negative header behaves:

```sh
node lab.mjs inspect -1 -32768 0x8000
```

For every negative count, `len > 64` is false. The program skips the return inside the check. It converts the count on the next line and proposes between 32,768 and 65,535 bytes.

## Fix the check with a single unsigned cast

`fixed_plan_copy` checks the converted count. The suffix `u` in `64u` means unsigned. A returned size of zero means copy no bytes, whether the request was empty or rejected.

```c
size_t fixed_plan_copy(int16_t len) {
    if ((uint16_t)len > 64u) {
        return 0;
    }
    return (uint16_t)len;
}
```

Casting `len` to `uint16_t` before the comparison forces both the test and the return value to share one number space (`0..65535`). Any bit pattern with bit 15 set becomes `32,768` or higher, triggers `> 64u`, and returns `0`. Lesson 2 uses a debugger called `lldb` to inspect the compiled program. You will see how the processor chooses which comparison result to use.
