Commit 828006c
TASK-008: review-pass fixes (security + performance iter1)
Applies fixes from the iter1 review pass on the detail::body hierarchy:
file_body (CWE-367 / perf):
- Open + fstat moved to constructor; size() is now accurate immediately.
- Drops lseek(SEEK_END); materialize() uses st_size from fstat.
Closes the TOCTOU window between size discovery and the fd handed
to MHD_create_response_from_fd, and removes the side-effect on the
fd's read position.
- Adds destructor that closes fd_ only when MHD never took ownership
(materialized_ stays false until from_fd returns non-null).
deferred_body (CWE-476):
- trampoline() guards against null cls and empty producer_ before
invoking the std::function. MHD's callback path doesn't catch C++
exceptions, so a bad_function_call would terminate in MHD's IO
thread; the guard returns MHD_CONTENT_READER_END_WITH_ERROR instead.
- Constructor asserts producer_ is non-empty (debug-only precondition).
Header docs:
- file_body: documents path-canonicalisation contract (O_NOFOLLOW only
blocks the final component) and fd ownership lifecycle.
- iovec_body: documents the borrowed-pointer lifetime contract
(iov_base buffers must outlive the MHD_Response*) and the heap
allocation note from DR-005.
- deferred_body: documents the std::function SBO caveat — capturing
more than the implementation-defined threshold silently heap-allocates.
Tests:
- file_body_size_known_before_materialize: size() must be correct at
construction (21 bytes for test_content), not only after materialize.
- deferred_body_trampoline_null_cls_returns_error: trampoline with
cls==nullptr returns MHD_CONTENT_READER_END_WITH_ERROR rather than
dereferencing.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 13f0818 commit 828006c
3 files changed
Lines changed: 143 additions & 28 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
105 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
106 | 109 | | |
107 | | - | |
| 110 | + | |
| 111 | + | |
108 | 112 | | |
109 | | - | |
| 113 | + | |
110 | 114 | | |
111 | | - | |
| 115 | + | |
112 | 116 | | |
113 | | - | |
| 117 | + | |
114 | 118 | | |
115 | 119 | | |
116 | | - | |
117 | | - | |
118 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
119 | 124 | | |
120 | 125 | | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
125 | 136 | | |
| 137 | + | |
126 | 138 | | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
131 | 148 | | |
132 | | - | |
133 | | - | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
134 | 153 | | |
135 | 154 | | |
136 | 155 | | |
| |||
177 | 196 | | |
178 | 197 | | |
179 | 198 | | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
180 | 203 | | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
181 | 207 | | |
182 | 208 | | |
183 | 209 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
116 | 117 | | |
117 | 118 | | |
118 | 119 | | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
125 | 140 | | |
126 | 141 | | |
127 | 142 | | |
128 | | - | |
| 143 | + | |
| 144 | + | |
129 | 145 | | |
130 | 146 | | |
131 | | - | |
| 147 | + | |
132 | 148 | | |
133 | 149 | | |
134 | 150 | | |
135 | 151 | | |
136 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
137 | 155 | | |
138 | 156 | | |
139 | 157 | | |
| |||
145 | 163 | | |
146 | 164 | | |
147 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
148 | 189 | | |
149 | 190 | | |
150 | 191 | | |
| |||
200 | 241 | | |
201 | 242 | | |
202 | 243 | | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
203 | 262 | | |
204 | 263 | | |
205 | 264 | | |
206 | 265 | | |
207 | 266 | | |
208 | 267 | | |
209 | 268 | | |
210 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
211 | 277 | | |
212 | 278 | | |
213 | 279 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
155 | 167 | | |
156 | 168 | | |
157 | 169 | | |
| |||
248 | 260 | | |
249 | 261 | | |
250 | 262 | | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
251 | 274 | | |
252 | 275 | | |
253 | 276 | | |
| |||
0 commit comments