Commit 46311fa
committed
fix: simplify dem_string_append logic and improve buffer handling
fix: improve dem_string_append handling for overlapping buffers
perf: optimize dem_string_append overlap detection
Improvements:
1. Add early return for empty strings (len == 0)
2. Fix overlap detection to check against ds->len instead of ds->cap
- Only the USED portion [ds->buf, ds->buf + ds->len) can cause overlap
- Checking against capacity was unnecessarily conservative
3. Fix boundary condition: use >= and <= instead of > and <
- Adjacent non-overlapping ranges should use fast path
This reduces unnecessary memory allocations in the common case where
strings don't overlap with the destination buffer.
fix: avoid undefined behavior on null pointer arithmetic in dem_string_append
When ds->buf is NULL, performing pointer arithmetic (ds->buf + ds->cap)
triggers UBSAN: 'applying zero offset to null pointer'.
Fix by checking if ds->buf is NULL before doing pointer comparisons.
If buf is NULL, we can safely append directly since there's no overlap.1 parent 684bc62 commit 46311fa
1 file changed
+21
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
| 287 | + | |
295 | 288 | | |
296 | 289 | | |
297 | 290 | | |
| |||
315 | 308 | | |
316 | 309 | | |
317 | 310 | | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
318 | 328 | | |
| 329 | + | |
319 | 330 | | |
320 | 331 | | |
321 | 332 | | |
322 | 333 | | |
323 | 334 | | |
324 | 335 | | |
| 336 | + | |
| 337 | + | |
325 | 338 | | |
326 | 339 | | |
327 | 340 | | |
| |||
0 commit comments