Fix potential segfault in term_trim() functions

...if requested width is negative.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2011-07-21 14:06:26 +04:00 committed by Slava Zanko
parent a1e34b8dfa
commit 1c0e5a4773
3 changed files with 50 additions and 41 deletions

View File

@ -381,33 +381,32 @@ str_8bit_term_trim (const char *text, int width)
actual = result;
remain = sizeof (result);
if (width < (int) length)
if (width > 0)
{
if (width <= 3)
if (width < (int) length)
{
memset (actual, '.', width);
actual += width;
remain -= width;
if (width <= 3)
{
memset (actual, '.', width);
actual += width;
remain -= width;
}
else
{
memset (actual, '.', 3);
actual += 3;
remain -= 3;
pos += length - width + 3;
for (; pos < length && remain > 1; pos++, actual++, remain--)
actual[0] = char_isprint (text[pos]) ? text[pos] : '.';
}
}
else
{
memset (actual, '.', 3);
actual += 3;
remain -= 3;
pos += length - width + 3;
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = char_isprint (text[pos]) ? text[pos] : '.';
}
}
}
else
{
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = char_isprint (text[pos]) ? text[pos] : '.';
}
}

View File

@ -327,23 +327,36 @@ str_ascii_term_trim (const char *text, int width)
actual = result;
remain = sizeof (result);
if (width < (int) length)
if (width > 0)
{
if (width <= 3)
if (width < (int) length)
{
memset (actual, '.', width);
actual += width;
remain -= width;
if (width <= 3)
{
memset (actual, '.', width);
actual += width;
remain -= width;
}
else
{
memset (actual, '.', 3);
actual += 3;
remain -= 3;
pos += length - width + 3;
/* copy suffix of text */
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
}
}
}
else
{
memset (actual, '.', 3);
actual += 3;
remain -= 3;
pos += length - width + 3;
/* copy suffix of text */
/* copy all characters */
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
@ -351,15 +364,6 @@ str_ascii_term_trim (const char *text, int width)
}
}
}
else
{
/* copy all characters */
for (; pos < length && remain > 1; pos++, actual++, remain--)
{
actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
}
}
actual[0] = '\0';
return result;

View File

@ -669,6 +669,12 @@ str_utf8_term_trim (const char *text, int width)
const struct term_form *pre_form;
struct utf8_tool tool;
if (width < 1)
{
result [0] = '\0';
return result;
}
pre_form = str_utf8_make_make_term_form (text, (size_t) (-1));
tool.cheked = pre_form->text;