Don't accept strings too long to fit in char[N] with trailing NUL.

It was previously possible to assign "hello" to a variable backed by char[5]
storage in C, and the array contained the correct character data but without
the trailing NUL, which was unexpected in C.

This is not allowed any more, only "helo" can fit into a char[5] now and
anything else fails the type check, just as it already happened for the longer
strings before.

Closes #122
This commit is contained in:
Vadim Zeitlin 2013-12-23 20:47:43 +01:00 committed by William S Fulton
commit cdf1ba9120
4 changed files with 13 additions and 7 deletions

View file

@ -537,7 +537,8 @@ SWIG_As##CharName##Array(SWIG_Object obj, Char *val, size_t size)
Char* cptr = 0; size_t csize = 0; int alloc = SWIG_OLDOBJ;
int res = SWIG_AsCharPtrAndSize(obj, &cptr, &csize, &alloc);
if (SWIG_IsOK(res)) {
if ((csize == size + 1) && cptr && !(cptr[csize-1])) --csize;
/* special case of single char conversion when we don't need space for NUL */
if (size == 1 && csize == 2 && cptr && !cptr[1]) --csize;
if (csize <= size) {
if (val) {
if (csize) memcpy(val, cptr, csize*sizeof(Char));