Merge pull request #9246 from keszybz/ellipsize-invalid-mem-ref

Fix invalid memory reference in ellipsize_mem()
This commit is contained in:
Lennart Poettering
2018-06-11 12:52:38 +02:00
committed by GitHub
7 changed files with 51 additions and 9 deletions

View File

@@ -882,8 +882,8 @@ static int table_data_requested_width(TableData *d, size_t *ret) {
return 0;
}
static char *align_string_mem(const char *str, size_t old_length, size_t new_length, unsigned percent) {
size_t w = 0, space, lspace;
static char *align_string_mem(const char *str, size_t new_length, unsigned percent) {
size_t w = 0, space, lspace, old_length;
const char *p;
char *ret;
size_t i;
@@ -893,8 +893,7 @@ static char *align_string_mem(const char *str, size_t old_length, size_t new_len
assert(str);
assert(percent <= 100);
if (old_length == (size_t) -1)
old_length = strlen(str);
old_length = strlen(str);
/* Determine current width on screen */
p = str;
@@ -1174,7 +1173,7 @@ int table_print(Table *t, FILE *f) {
if (l > width[j]) {
/* Field is wider than allocated space. Let's ellipsize */
buffer = ellipsize_mem(field, (size_t) -1, width[j], d->ellipsize_percent);
buffer = ellipsize(field, width[j], d->ellipsize_percent);
if (!buffer)
return -ENOMEM;
@@ -1183,7 +1182,7 @@ int table_print(Table *t, FILE *f) {
} else if (l < width[j]) {
/* Field is shorter than allocated space. Let's align with spaces */
buffer = align_string_mem(field, (size_t) -1, width[j], d->align_percent);
buffer = align_string_mem(field, width[j], d->align_percent);
if (!buffer)
return -ENOMEM;

View File

@@ -541,7 +541,7 @@ char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigne
return strdup("");
/* If no multibyte characters use ascii_ellipsize_mem for speed */
if (ascii_is_valid(s))
if (ascii_is_valid_n(s, old_length))
return ascii_ellipsize_mem(s, old_length, new_length, percent);
x = ((new_length - 1) * percent) / 100;

View File

@@ -247,6 +247,9 @@ char *utf8_escape_non_printable(const char *str) {
char *ascii_is_valid(const char *str) {
const char *p;
/* Check whether the string consists of valid ASCII bytes,
* i.e values between 0 and 127, inclusive. */
assert(str);
for (p = str; *p; p++)
@@ -256,6 +259,21 @@ char *ascii_is_valid(const char *str) {
return (char*) str;
}
char *ascii_is_valid_n(const char *str, size_t len) {
size_t i;
/* Very similar to ascii_is_valid(), but checks exactly len
* bytes and rejects any NULs in that range. */
assert(str);
for (i = 0; i < len; i++)
if ((unsigned char) str[i] >= 128 || str[i] == 0)
return NULL;
return (char*) str;
}
/**
* utf8_encode_unichar() - Encode single UCS-4 character as UTF-8
* @out_utf8: output buffer of at least 4 bytes or NULL

View File

@@ -22,6 +22,7 @@ bool unichar_is_valid(char32_t c);
const char *utf8_is_valid(const char *s) _pure_;
char *ascii_is_valid(const char *s) _pure_;
char *ascii_is_valid_n(const char *str, size_t len);
bool utf8_is_printable_newline(const char* str, size_t length, bool newline) _pure_;
#define utf8_is_printable(str, length) utf8_is_printable_newline(str, length, true)