Skip to content

Backport !1199 “gunicode: Fix UB in gutf8.c and utf8-pointer test” to glib-2-62

In glib/gutf8.c there was an UB in function g_utf8_find_prev_char when p == str. In this case we substract one from p and now p points to a location outside of the boundary of str. It's a UB by the standard. Since this function are meant to be fast, we don't check the boundary conditions.

Fix glib/tests/utf8-pointer test. It failed due to the UB described above and aggressive optimisation when -O2 and LTO are enabled. Some compilers (e.g. GCC with major version >= 8) create an optimised version of g_utf8_find_prev_char with the first argument fixed and stored somewhere else (with a different pointer). It can be solved with either marking str as volatile or creating a copy of str in memory. We choose the second approach since it's more explicit solution.

Add additional checks to glib/tests/utf8-pointer test.

Closes #1917 (closed)

Trivial backport of !1199 (merged).

Merge request reports