Replace broken str* functions with safe versions.

This commit is contained in:
Erik de Castro Lopo
2013-03-19 20:23:44 +11:00
parent 15a011c43d
commit 2d6354ff2a
11 changed files with 96 additions and 61 deletions

View File

@@ -119,7 +119,7 @@ void test_utf8()
assert(charset_mbtowc(charset, &wc, "\377\277\277\277\277\277", 9) == -1);
/* Encoder */
strcpy(s, ".......");
safe_strncpy(s, ".......", sizeof(s));
assert(charset_wctomb(charset, s, 1 << 31) == -1 &&
!strcmp(s, "......."));
assert(charset_wctomb(charset, s, 127) == 1 &&
@@ -161,7 +161,7 @@ void test_ascii()
assert(charset_mbtowc(charset, &wc, "\200", 2) == -1);
/* Encoder */
strcpy(s, "..");
safe_strncpy(s, "..", sizeof(s));
assert(charset_wctomb(charset, s, 256) == -1 && !strcmp(s, ".."));
assert(charset_wctomb(charset, s, 255) == -1);
assert(charset_wctomb(charset, s, 128) == -1);
@@ -182,7 +182,7 @@ void test_iso1()
assert(charset_mbtowc(charset, &wc, "\302\200", 9) == 1 && wc == 0xc2);
/* Encoder */
strcpy(s, "..");
safe_strncpy(s, "..", sizeof(s));
assert(charset_wctomb(charset, s, 256) == -1 && !strcmp(s, ".."));
assert(charset_wctomb(charset, s, 255) == 1 && !strcmp(s, "\377."));
assert(charset_wctomb(charset, s, 128) == 1 && !strcmp(s, "\200."));
@@ -203,7 +203,7 @@ void test_iso2()
assert(charset_mbtowc(charset, &wc, "\377", 2) == 1 && wc == 0x2d9);
/* Encoder */
strcpy(s, "..");
safe_strncpy(s, "..", sizeof(s));
assert(charset_wctomb(charset, s, 256) == -1 && !strcmp(s, ".."));
assert(charset_wctomb(charset, s, 255) == -1 && !strcmp(s, ".."));
assert(charset_wctomb(charset, s, 258) == 1 && !strcmp(s, "\303."));
@@ -230,7 +230,7 @@ void test_convert()
assert(charset_convert("UTF-8", "iso-8859-1",
"\302\200\304\200x", 5, &q, &n) == 1 &&
n == 3 && !strcmp(q, "\200?x"));
assert(charset_convert("iso-8859-1", "UTF-8",
assert(charset_convert("iso-8859-1", "UTF-8",
"\000\200\377", 3, &q, &n) == 0 &&
n == 5 && !memcmp(q, "\000\302\200\303\277", 5));
assert(charset_convert("iso-8859-1", "iso-8859-1",

View File

@@ -30,6 +30,7 @@
#include "iconvert.h"
#include "share/alloc.h"
#include "share/safe_str.h"
/*
* Convert data from one encoding to another. Return:
@@ -76,18 +77,18 @@ int iconvert(const char *fromcode, const char *tocode,
tocode[4] != '8' ||
tocode[5] != '\0') {
char *tocode1;
size_t dest_len = strlen(tocode) + 11;
/*
* Try using this non-standard feature of glibc and libiconv.
* This is deliberately not a config option as people often
* change their iconv library without rebuilding applications.
*/
tocode1 = safe_malloc_add_2op_(strlen(tocode), /*+*/11);
tocode1 = safe_malloc_(dest_len);
if (!tocode1)
goto fail;
strcpy(tocode1, tocode);
strcat(tocode1, "//TRANSLIT");
safe_strncpy(tocode1, tocode, dest_len);
safe_strncat(tocode1, "//TRANSLIT", dest_len);
cd2 = iconv_open(tocode1, "UTF-8");
free(tocode1);

View File

@@ -31,6 +31,7 @@
#include <string.h>
#include "share/alloc.h"
#include "share/safe_str.h"
#include "utf8.h"
#include "charset.h"
@@ -298,7 +299,7 @@ static int convert_string(const char *fromcode, const char *tocode,
s = safe_malloc_add_2op_(fromlen, /*+*/1);
if (!s)
return -1;
strcpy(s, from);
safe_strncpy(s, from, fromlen + 1);
*to = s;
for (; *s; s++)
if (*s & ~0x7f)