diff options
author | Campbell Barton <ideasman42@gmail.com> | 2012-10-31 08:28:49 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2012-10-31 08:28:49 +0400 |
commit | fd2907a0b656f06c90243a5956df44d47d4bddd6 (patch) | |
tree | 1795fc58c0aea430d1d0ffdb7c9a7db6cf05fb5a /source/blender/blenlib/intern | |
parent | ac86beb0e569075e2bd6e2da914bda2831b06c18 (diff) |
add assert if zero is passed to string copy functions, would copy into first byte anyway.
Diffstat (limited to 'source/blender/blenlib/intern')
-rw-r--r-- | source/blender/blenlib/intern/string.c | 13 | ||||
-rw-r--r-- | source/blender/blenlib/intern/string_utf8.c | 6 |
2 files changed, 14 insertions, 5 deletions
diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c index 73d88ddd259..ff589764287 100644 --- a/source/blender/blenlib/intern/string.c +++ b/source/blender/blenlib/intern/string.c @@ -41,6 +41,8 @@ #include "BLI_dynstr.h" #include "BLI_string.h" +#include "BLI_utildefines.h" + char *BLI_strdupn(const char *str, const size_t len) { char *n = MEM_mallocN(len + 1, "strdup"); @@ -71,6 +73,7 @@ char *BLI_strncpy(char *dst, const char *src, const size_t maxncpy) { size_t srclen = strlen(src); size_t cpylen = (srclen > (maxncpy - 1)) ? (maxncpy - 1) : srclen; + BLI_assert(maxncpy != 0); memcpy(dst, src, cpylen); dst[cpylen] = '\0'; @@ -130,10 +133,13 @@ char *BLI_sprintfN(const char *format, ...) * TODO: support more fancy string escaping. current code is primitive * this basically is an ascii version of PyUnicode_EncodeUnicodeEscape() * which is a useful reference. */ -size_t BLI_strescape(char *dst, const char *src, const size_t maxlen) +size_t BLI_strescape(char *dst, const char *src, const size_t maxncpy) { size_t len = 0; - while (len < maxlen) { + + BLI_assert(maxncpy != 0); + + while (len < maxncpy) { switch (*src) { case '\0': goto escape_finish; @@ -144,7 +150,7 @@ size_t BLI_strescape(char *dst, const char *src, const size_t maxlen) case '\t': case '\n': case '\r': - if (len + 1 < maxlen) { + if (len + 1 < maxncpy) { *dst++ = '\\'; len++; } @@ -439,4 +445,3 @@ void BLI_ascii_strtoupper(char *str, const size_t len) if (str[i] >= 'a' && str[i] <= 'z') str[i] -= 'a' - 'A'; } - diff --git a/source/blender/blenlib/intern/string_utf8.c b/source/blender/blenlib/intern/string_utf8.c index a8960f1abbb..59f8e7458e1 100644 --- a/source/blender/blenlib/intern/string_utf8.c +++ b/source/blender/blenlib/intern/string_utf8.c @@ -33,8 +33,12 @@ #include <string.h> #include <wchar.h> #include <wctype.h> +#include <stdio.h> +#include <stdlib.h> -#include "BLI_string_utf8.h" +#include "BLI_utildefines.h" + +#include "BLI_string_utf8.h" /* own include */ /* from libswish3, originally called u8_isvalid(), * modified to return the index of the bad character (byte index not utf). |