diff options
author | Pádraig Brady <P@draigBrady.com> | 2010-03-19 21:40:05 +0000 |
---|---|---|
committer | Pádraig Brady <P@draigBrady.com> | 2010-03-19 21:40:05 +0000 |
commit | c872a1d8a51617c2ec76423da6f76ef2208f30d9 (patch) | |
tree | 379df526baf20883e9497bfe574ec9b051d48c88 | |
parent | dfe0d336a00940c8e13c24b6d5d7485a2d7310b0 (diff) | |
download | coreutils-c872a1d8a51617c2ec76423da6f76ef2208f30d9.tar.xz |
maint: mbsalign: fix an edge case where we truncate too much
* gl/lib/mbsalign.c (mbsalign): Ensure the temporary destination buffer
is big enough, as it may need to be bigger than the source buffer
in the presence of single byte non printable chars.
* gl/tests/test-mbsalign.c (main): Add a test to trigger the issue.
-rw-r--r-- | gl/lib/mbsalign.c | 6 | ||||
-rw-r--r-- | gl/tests/test-mbsalign.c | 7 |
2 files changed, 13 insertions, 0 deletions
diff --git a/gl/lib/mbsalign.c b/gl/lib/mbsalign.c index 5b55ec2d0..b92fe8f79 100644 --- a/gl/lib/mbsalign.c +++ b/gl/lib/mbsalign.c @@ -178,6 +178,12 @@ mbsalign (const char *src, char *dest, size_t dest_size, then create a modified copy of it. */ if (wc_enabled && (conversion || (n_cols > *width))) { + if (conversion) + { + /* May have increased the size by converting + \t to \uFFFD for example. */ + src_size = wcstombs (NULL, str_wc, 0) + 1; + } newstr = malloc (src_size); if (newstr == NULL) { diff --git a/gl/tests/test-mbsalign.c b/gl/tests/test-mbsalign.c index 9f8935732..1d894831e 100644 --- a/gl/tests/test-mbsalign.c +++ b/gl/tests/test-mbsalign.c @@ -87,6 +87,13 @@ main (void) width = 4; /* cells */ n = mbsalign ("¹²³", dest, 0, &width, MBS_ALIGN_LEFT, 0); ASSERT (width == 3); + + /* Test case where output is larger than input + (as tab converted to multi byte replacement char). */ + width = 4; + n = mbsalign ("t\tés" /* 6 including NUL */ , dest, sizeof dest, + &width, MBS_ALIGN_LEFT, 0); + ASSERT (n == 7); } return 0; |