Index: Python/import.c =================================================================== --- Python/import.c (revision 60353) +++ Python/import.c (working copy) @@ -2120,13 +2120,15 @@ if ((pkgname != NULL) && (pkgname != Py_None)) { /* __package__ is set, so use it */ + char *pkgname_str; Py_ssize_t len; + if (!PyUnicode_Check(pkgname)) { PyErr_SetString(PyExc_ValueError, "__package__ set to non-string"); return NULL; } - len = PyUnicode_GET_SIZE(pkgname); + pkgname_str = PyUnicode_AsStringAndSize(pkgname, &len); if (len == 0) { if (level > 0) { PyErr_SetString(PyExc_ValueError, @@ -2140,7 +2142,7 @@ "Package name too long"); return NULL; } - strcpy(buf, PyUnicode_AsString(pkgname)); + strcpy(buf, pkgname_str); } else { /* __package__ not set, so figure it out and set it */ modname = PyDict_GetItem(globals, namestr); @@ -2150,14 +2152,17 @@ modpath = PyDict_GetItem(globals, pathstr); if (modpath != NULL) { /* __path__ is set, so modname is already the package name */ - Py_ssize_t len = PyUnicode_GET_SIZE(modname); + char *modname_str; + Py_ssize_t len; int error; + + modname_str = PyUnicode_AsStringAndSize(modname, &len); if (len > MAXPATHLEN) { PyErr_SetString(PyExc_ValueError, "Module name too long"); return NULL; } - strcpy(buf, PyUnicode_AsString(modname)); + strcpy(buf, modname_str); error = PyDict_SetItem(globals, pkgstr, modname); if (error) { PyErr_SetString(PyExc_ValueError, @@ -2184,7 +2189,7 @@ } return Py_None; } - len = lastdot - start; + len = (size_t)(lastdot - start); if (len >= MAXPATHLEN) { PyErr_SetString(PyExc_ValueError, "Module name too long"); Index: Python/structmember.c =================================================================== --- Python/structmember.c (revision 60353) +++ Python/structmember.c (working copy) @@ -239,15 +239,22 @@ *(PyObject **)addr = v; Py_XDECREF(oldv); break; - case T_CHAR: - if (PyUnicode_Check(v) && PyUnicode_GetSize(v) == 1) { - *(char*)addr = PyUnicode_AsString(v)[0]; + case T_CHAR: { + char *string; + Py_ssize_t len; + + if (!PyUnicode_Check(v)) { + PyErr_BadArgument(); + return -1; } - else { + string = PyUnicode_AsStringAndSize(v, &len); + if (len != 1) { PyErr_BadArgument(); return -1; } + *(char*)addr = string[0]; break; + } #ifdef HAVE_LONG_LONG case T_LONGLONG:{ PY_LONG_LONG value; Index: Objects/typeobject.c =================================================================== --- Objects/typeobject.c (revision 60353) +++ Objects/typeobject.c (working copy) @@ -1207,7 +1207,7 @@ if (PyList_GET_ITEM(list, j) == o) { o = class_name(o); PyErr_Format(PyExc_TypeError, - "duplicate base class %s", + "duplicate base class %.400s", o ? PyUnicode_AsString(o) : "?"); Py_XDECREF(o); return -1; @@ -2085,20 +2085,22 @@ { PyObject *doc = PyDict_GetItemString(dict, "__doc__"); if (doc != NULL && PyUnicode_Check(doc)) { - size_t n; + Py_ssize_t len; + char *doc_str; char *tp_doc; - const char *str = PyUnicode_AsString(doc); - if (str == NULL) { + + doc_str = PyUnicode_AsStringAndSize(doc, &len); + if (doc_str == NULL) { Py_DECREF(type); return NULL; } - n = strlen(str); - tp_doc = (char *)PyObject_MALLOC(n+1); + tp_doc = (char *)PyObject_MALLOC(len + 1); if (tp_doc == NULL) { Py_DECREF(type); return NULL; } - memcpy(tp_doc, str, n+1); + memcpy(tp_doc, doc_str, len); + tp_doc[len] = '\0'; type->tp_doc = tp_doc; } } Index: Objects/structseq.c =================================================================== --- Objects/structseq.c (revision 60353) +++ Objects/structseq.c (working copy) @@ -231,9 +231,10 @@ structseq_repr(PyStructSequence *obj) { /* buffer and type size were chosen well considered. */ -#define REPR_BUFFER_SIZE 512 -#define TYPE_MAXSIZE 100 - + enum { + REPR_BUFFER_SIZE = 512, + TYPE_MAXSIZE = 100, + }; PyObject *tup; PyTypeObject *typ = Py_TYPE(obj); int i, removelast = 0; @@ -255,9 +256,10 @@ pbuf += len; *pbuf++ = '('; - for (i=0; i < VISIBLE_SIZE(obj); i++) { + for (i = 0; i < VISIBLE_SIZE(obj); i++) { PyObject *val, *repr; char *cname, *crepr; + Py_ssize_t crepr_size; cname = typ->tp_members[i].name; @@ -270,7 +272,9 @@ Py_DECREF(tup); return NULL; } - crepr = PyUnicode_AsString(repr); + /* crepr could include null-bytes, so don't use strlen() to + get its size. */ + crepr = PyUnicode_AsStringAndSize(repr, &crepr_size); if (crepr == NULL) { Py_DECREF(tup); Py_DECREF(repr); @@ -278,13 +282,13 @@ } /* + 3: keep space for "=" and ", " */ - len = strlen(cname) + strlen(crepr) + 3; + len = strlen(cname) + crepr_size + 3; if ((pbuf+len) <= endofbuf) { strcpy(pbuf, cname); pbuf += strlen(cname); *pbuf++ = '='; strcpy(pbuf, crepr); - pbuf += strlen(crepr); + pbuf += crepr_size; *pbuf++ = ','; *pbuf++ = ' '; removelast = 1; @@ -301,7 +305,7 @@ Py_DECREF(tup); if (removelast) { /* overwrite last ", " */ - pbuf-=2; + pbuf -= 2; } *pbuf++ = ')'; *pbuf = '\0'; Index: Modules/datetimemodule.c =================================================================== --- Modules/datetimemodule.c (revision 60353) +++ Modules/datetimemodule.c (working copy) @@ -1202,10 +1202,9 @@ assert(object && format && timetuple); assert(PyUnicode_Check(format)); /* Convert the input format to a C string and size */ - pin = PyUnicode_AsString(format); + pin = PyUnicode_AsStringAndSize(format, &flen); if (!pin) return NULL; - flen = PyUnicode_GetSize(format); /* Give up if the year is before 1900. * Python strftime() plays games with the year, and different Index: Modules/zipimport.c =================================================================== --- Modules/zipimport.c (revision 60353) +++ Modules/zipimport.c (working copy) @@ -61,16 +61,14 @@ zipimporter_init(ZipImporter *self, PyObject *args, PyObject *kwds) { char *path, *p, *prefix, buf[MAXPATHLEN+2]; - size_t len; + Py_ssize_t len; if (!_PyArg_NoKeywords("zipimporter()", kwds)) return -1; - if (!PyArg_ParseTuple(args, "s:zipimporter", - &path)) + if (!PyArg_ParseTuple(args, "s#:zipimporter", &path, &len)) return -1; - len = strlen(path); if (len == 0) { PyErr_SetString(ZipImportError, "archive path is empty"); return -1; @@ -388,6 +386,7 @@ #endif PyObject *toc_entry; Py_ssize_t len; + char *archive_str; if (!PyArg_ParseTuple(args, "s:zipimporter.get_data", &path)) return NULL; @@ -404,9 +403,9 @@ } path = buf; #endif - len = PyUnicode_GET_SIZE(self->archive); + archive_str = PyUnicode_AsStringAndSize(self->archive, &len); if ((size_t)len < strlen(path) && - strncmp(path, PyUnicode_AsString(self->archive), len) == 0 && + strncmp(path, archive_str, len) == 0 && path[len] == SEP) { path = path + len + 1; } @@ -416,7 +415,7 @@ PyErr_SetFromErrnoWithFilename(PyExc_IOError, path); return NULL; } - return get_data(PyUnicode_AsString(self->archive), toc_entry); + return get_data(archive_str, toc_entry); } static PyObject * Index: Modules/timemodule.c =================================================================== --- Modules/timemodule.c (revision 60353) +++ Modules/timemodule.c (working copy) @@ -424,7 +424,8 @@ struct tm buf; const char *fmt; PyObject *format; - size_t fmtlen, buflen; + Py_ssize_t fmtlen; + size_t buflen; char *outbuf = 0; size_t i; @@ -508,11 +509,9 @@ return NULL; } - /* Convert the unicode string to an ascii one */ - fmt = PyUnicode_AsString(format); + /* Convert the unicode string to an ascii one */ + fmt = PyUnicode_AsStringAndSize(format, &fmtlen); - fmtlen = strlen(fmt); - /* I hate these functions that presume you know how big the output * will be ahead of time... */ Index: Modules/parsermodule.c =================================================================== --- Modules/parsermodule.c (revision 60353) +++ Modules/parsermodule.c (working copy) @@ -717,11 +717,11 @@ Py_DECREF(o); } } - temp_str = PyUnicode_AsString(temp); - len = PyUnicode_GET_SIZE(temp) + 1; - strn = (char *)PyObject_MALLOC(len); + temp_str = PyUnicode_AsStringAndSize(temp, &len); + strn = (char *)PyObject_MALLOC(len + 1); if (strn != NULL) - (void) memcpy(strn, temp_str, len); + (void) memcpy(strn, temp_str, len + 1); + strn[len] = '\0'; Py_DECREF(temp); } else if (!ISNONTERMINAL(type)) { @@ -807,11 +807,11 @@ if (res && encoding) { Py_ssize_t len; const char *temp; - temp = PyUnicode_AsString(encoding); - len = PyUnicode_GET_SIZE(encoding) + 1; - res->n_str = (char *)PyObject_MALLOC(len); + temp = PyUnicode_AsStringAndSize(encoding, &len); + res->n_str = (char *)PyObject_MALLOC(len + 1); if (res->n_str != NULL && temp != NULL) - (void) memcpy(res->n_str, temp, len); + (void) memcpy(res->n_str, temp, len + 1); + res->n_str[len] = '\0'; Py_DECREF(encoding); Py_DECREF(tuple); } Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (revision 60353) +++ Modules/posixmodule.c (working copy) @@ -5533,7 +5533,6 @@ size_t hi = tablesize; int cmp; const char *confname; - Py_ssize_t namelen; if (!PyUnicode_Check(arg)) { PyErr_SetString(PyExc_TypeError, "configuration names must be strings or integers"); @@ -5542,7 +5541,6 @@ confname = PyUnicode_AsString(arg); if (confname == NULL) return 0; - namelen = strlen(confname); while (lo < hi) { mid = (lo + hi) / 2; cmp = strcmp(confname, table[mid].name);