mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-08-19 09:40:21 +00:00
* Merge commit from fork * [core] Fix XML escape encoder overrun and unsigned-char UTF-8 gate `switch_xml_ampencode()` had two independent defects in its UTF-8 numeric-escape path. Buffer overrun: the encoder grows its destination once per source byte, but the realloc margin reserved only the 10 data characters of the widest escape `"&#x%X;"` (a 21-bit code point rendered as 6 hex digits), not the terminating NUL that `sprintf` also writes. At the margin boundary that NUL landed one byte past the allocation. Reserve 11 bytes in the guard (10 data chars plus the NUL) and emit the escape with `snprintf` bounded to the remaining space, so the write stays in bounds even if the margin is ever miscounted. The other escape sinks are all within the widened margin and are unchanged. Char signedness: the lead-byte test `(*s >> 8) & 0x01` reads bit 8 of a plain `char`, which exists only after sign extension. Where `char` is signed the high bit sign-extends and the test passes; where `char` is unsigned it is always zero, so the numeric-escape path never ran and multi-byte UTF-8 was emitted raw, making serialized XML differ by architecture. Test bit 7 directly with `(*s & 0x80)`, correct regardless of `char` signedness. This also makes the overrun fix effective on unsigned-`char` builds, where the escape path now runs. Add unit test `test_utf_8_wide_codepoint`, which serializes U+10FFFF and long runs of it across buffer reallocations, sweeping an ASCII prefix so an escape is emitted at the minimum-headroom offset, and asserts the full serialized length.
FreeSWITCH unit tests should be kept as shallow unit tests and micro benchmarks testing functionality exposed through libfreeswitch. Requirements for a new unit tests: 1. Tests must use switch_test.h framework