diff options
author | Patrick Steinhardt <ps@pks.im> | 2022-12-01 15:46:53 +0100 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2022-12-09 06:26:21 +0100 |
commit | 522cc87fdc25449222a5894a428eebf4b8d5eaa9 (patch) | |
tree | 05b0e6b3fc523e14d7eb612abe30520681313a26 /utf8.h | |
parent | pretty: fix integer overflow in wrapping format (diff) | |
download | git-522cc87fdc25449222a5894a428eebf4b8d5eaa9.tar.xz git-522cc87fdc25449222a5894a428eebf4b8d5eaa9.zip |
utf8: fix truncated string lengths in `utf8_strnwidth()`
The `utf8_strnwidth()` function accepts an optional string length as
input parameter. This parameter can either be set to `-1`, in which case
we call `strlen()` on the input. Or it can be set to a positive integer
that indicates a precomputed length, which callers typically compute by
calling `strlen()` at some point themselves.
The input parameter is an `int` though, whereas `strlen()` returns a
`size_t`. This can lead to implementation-defined behaviour though when
the `size_t` cannot be represented by the `int`. In the general case
though this leads to wrap-around and thus to negative string sizes,
which is sure enough to not lead to well-defined behaviour.
Fix this by accepting a `size_t` instead of an `int` as string length.
While this takes away the ability of callers to simply pass in `-1` as
string length, it really is trivial enough to convert them to instead
pass in `strlen()` instead.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'utf8.h')
-rw-r--r-- | utf8.h | 2 |
1 files changed, 1 insertions, 1 deletions
@@ -7,7 +7,7 @@ typedef unsigned int ucs_char_t; /* assuming 32bit int */ size_t display_mode_esc_sequence_len(const char *s); int utf8_width(const char **start, size_t *remainder_p); -int utf8_strnwidth(const char *string, int len, int skip_ansi); +int utf8_strnwidth(const char *string, size_t len, int skip_ansi); int utf8_strwidth(const char *string); int is_utf8(const char *text); int is_encoding_utf8(const char *name); |