gvariant-parser: Add additional buffer byte for nul terminator

In `pattern_coalesce()`.

I am fairly sure this byte is never needed, as the most extreme case of
`max (strlen (left), strlen (right)` vs `strlen (left) + strlen (right)`
I can think of still gives 1 byte left in the buffer (without the need
for this commit).

However, the proof we have of how much space is needed in the buffer
only goes far enough to show that the number of bytes needed before the
nul terminator is at most `strlen (left) + strlen (right)`. That means
we still technically need to add an additional byte for the nul
terminator.

The need for this could be eliminated by coming up with a stronger proof
to show that the number of bytes needed is strictly less than `strlen
(left) + strlen (right)`, but I can’t do that, and adding one byte is
easy to do. Type strings are short and we’re nowhere near making a large
allocation here.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3469
This commit is contained in:
Philip Withnall 2024-09-19 17:44:28 +01:00
parent c7e3512902
commit 785b61cfcb
No known key found for this signature in database
GPG Key ID: DCDF5885B1F3ED73

View File

@ -445,7 +445,7 @@ pattern_coalesce (const gchar *left,
* This can be proven by the fact that `out` is never incremented by more
* bytes than are consumed from `left` or `right` in each iteration.
*/
buflen = strlen (left) + strlen (right);
buflen = strlen (left) + strlen (right) + 1;
out = result = g_malloc (buflen);
while (*left && *right)