Browse Source

string: avoid an infinite loop with some invalid UTF-8 sequences, and add a

unit test for the problem. Bug found by Bastian Märkisch <bmaerkisch@web.de>.
tags/v0.99.beta19
Sam Hocevar sam 12 years ago
parent
commit
97c26b9bbb
2 changed files with 16 additions and 6 deletions
  1. +6
    -6
      caca/string.c
  2. +10
    -0
      test/canvas.cpp

+ 6
- 6
caca/string.c View File

@@ -256,25 +256,25 @@ int caca_put_str(caca_canvas_t *cv, int x, int y, char const *s)
size_t rd;
int len = 0;

if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
if (y < 0 || y >= (int)cv->height || x >= (int)cv->width)
{
while(*s)
while (*s)
{
len += caca_utf32_is_fullwidth(caca_utf8_to_utf32(s, &rd)) ? 2 : 1;
s += rd;
s += rd ? rd : 1;
}
return len;
}

while(*s)
while (*s)
{
uint32_t ch = caca_utf8_to_utf32(s, &rd);

if(x + len >= -1 && x + len < (int)cv->width)
if (x + len >= -1 && x + len < (int)cv->width)
caca_put_char(cv, x + len, y, ch);

len += caca_utf32_is_fullwidth(ch) ? 2 : 1;
s += rd;
s += rd ? rd : 1;
}

return len;


+ 10
- 0
test/canvas.cpp View File

@@ -25,6 +25,7 @@ class CanvasTest : public CppUnit::TestCase
CPPUNIT_TEST(test_creation);
CPPUNIT_TEST(test_resize);
CPPUNIT_TEST(test_chars);
CPPUNIT_TEST(test_utf8);
CPPUNIT_TEST_SUITE_END();

public:
@@ -94,6 +95,15 @@ public:

caca_free_canvas(cv);
}

void test_utf8()
{
caca_canvas_t *cv;
cv = caca_create_canvas(10, 10);

/* Send only one byte of a 4-byte sequence */
caca_put_str(cv, 0, 0, "\xf0");
}
};

CPPUNIT_TEST_SUITE_REGISTRATION(CanvasTest);


Loading…
Cancel
Save