Security in depth · Unit 21 · Lesson 3 of 6
Arithmetic has representation limits
Check a derived allocation size against arithmetic and application limits.
Helpful before thisMemory safety: boundaries and lifetimes
After this lesson you can
- Separate a size calculation that exceeds its representation from one that exceeds a policy limit.
One idea. One situation. One reasoned decision.
How it works
An integer type has a range and defined or language-dependent behavior outside that range. Signedness, conversions, and multiplication can change a size calculation before it reaches a memory operation. Check arithmetic under the actual language rules and use checked operations where appropriate. A positive-looking input does not prove that every derived size remains valid.
Two limits apply to the same calculation
A fictional import component reserves space for fixed-size records. Its size field represents whole numbers from 0 through 65,535. The application’s allocation limit is 48,000 bytes. Assume no headers or terminators in this exercise.
| Request | Record count | Bytes per record |
|---|---|---|
| A | 3,000 | 24 |
| B | 2,100 | 24 |
Worked decision: A requires 72,000 bytes, which exceeds the size field’s range. B requires 50,400 bytes: representable in that field, but above the application limit. Neither request meets both requirements.
The maximum permitted whole-record count here is 2,000, because 2,000 × 24 equals 48,000. An implementation must validate the multiplication or establish an equivalent safe bound before relying on its result. Widening the size field could address A’s representation problem, but would not change the application’s policy.
Do not predict a universal result when arithmetic exceeds its range: behavior depends on the language, type, and operation. The review should name those rules and specify rejection or another documented handling path. Individually positive values are not enough evidence for a safe derived size.
The key distinction: Valid individual values do not guarantee a valid combined result.
Check yourself
No timer. No penalties. Read the explanation and try again whenever you like.
This lesson’s questions have changed. Your reading progress is saved; review the updated questions.
-
Which assessment correctly applies both limits?
Show the answer
Correct answer: A exceeds representation and policy; B exceeds policy even though its size is representable. The products are 72,000 and 50,400 bytes. Both exceed 48,000; only A also exceeds 65,535.
Try it
- WriteWrite the disposition for A and B, showing count times record size. Then calculate the largest whole-record count permitted by the stated application limit.