Commit 272f6a9
Fix integer underflow in Chakra napi_get_value_string_* zero-bufsize handling (#197)
## Summary
Fixes an integer underflow in the Chakra Node-API string getters when a
caller passes a non-null buffer with `bufsize == 0`.
`napi_get_value_string_utf16` forwarded `bufsize - 1` to
`JsCopyStringUtf16` as the destination capacity and stored the null
terminator at `buf[bufsize - 1]`. With `bufsize == 0`, `bufsize - 1`
underflows to `SIZE_MAX`, so:
- the **entire** JS string is copied into the zero-length buffer, and
- the terminator is written at `buf[SIZE_MAX]`.
That's an attacker-sized out-of-bounds write reachable from any native
caller that passes `(buf != nullptr, bufsize == 0)` (CWE-191 →
CWE-787/CWE-120).
## Fix
- `napi_get_value_string_utf16`: gate the copy on `bufsize != 0`
(mirroring the upstream Node implementation). A non-null buffer with
`bufsize == 0` now reports zero copied and writes nothing.
- `napi_get_value_string_latin1` / `napi_get_value_string_utf8`: these
shared a related, milder bug — with `bufsize == 0` they took the slow
path (a needless allocation) and stored the terminator at `buf[0]`, one
byte past a zero-length buffer. Both now short-circuit `bufsize == 0`
identically.
## Test
Adds a native regression test
(`NodeApi.GetValueStringUtf16HandlesZeroBufsize`) — the path isn't
reachable from JS, so it's exercised through the C API. It passes a
sentinel-filled buffer with `bufsize == 0` and asserts nothing is
written and the reported length is zero, while a normally-sized buffer
still copies and null-terminates. Guarded off the V8JSI backend (which
doesn't expose `napi_get_value_string_utf16`), same as the
`napi_create_dataview` regression test from #181.
**Verified (Win32/Chakra):** builds clean; the new test and the full
UnitTests suite pass.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 99457c0 commit 272f6a9
3 files changed
Lines changed: 93 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1405 | 1405 | | |
1406 | 1406 | | |
1407 | 1407 | | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
1408 | 1416 | | |
1409 | 1417 | | |
1410 | 1418 | | |
| |||
1489 | 1497 | | |
1490 | 1498 | | |
1491 | 1499 | | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
| 1507 | + | |
1492 | 1508 | | |
1493 | 1509 | | |
1494 | 1510 | | |
| |||
1574 | 1590 | | |
1575 | 1591 | | |
1576 | 1592 | | |
1577 | | - | |
| 1593 | + | |
1578 | 1594 | | |
1579 | 1595 | | |
1580 | 1596 | | |
| |||
1593 | 1609 | | |
1594 | 1610 | | |
1595 | 1611 | | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
1596 | 1618 | | |
1597 | 1619 | | |
1598 | 1620 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1815 | 1815 | | |
1816 | 1816 | | |
1817 | 1817 | | |
| 1818 | + | |
| 1819 | + | |
| 1820 | + | |
| 1821 | + | |
| 1822 | + | |
| 1823 | + | |
1818 | 1824 | | |
1819 | 1825 | | |
1820 | 1826 | | |
| |||
1844 | 1850 | | |
1845 | 1851 | | |
1846 | 1852 | | |
| 1853 | + | |
| 1854 | + | |
| 1855 | + | |
| 1856 | + | |
| 1857 | + | |
| 1858 | + | |
1847 | 1859 | | |
1848 | 1860 | | |
1849 | 1861 | | |
| |||
1873 | 1885 | | |
1874 | 1886 | | |
1875 | 1887 | | |
| 1888 | + | |
| 1889 | + | |
| 1890 | + | |
| 1891 | + | |
| 1892 | + | |
| 1893 | + | |
1876 | 1894 | | |
1877 | 1895 | | |
1878 | 1896 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
351 | 351 | | |
352 | 352 | | |
353 | 353 | | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
354 | 406 | | |
355 | 407 | | |
356 | 408 | | |
| |||
0 commit comments