summaryrefslogtreecommitdiffstats
path: root/Objects/memoryobject.c
diff options
context:
space:
mode:
authorStefan Krah <skrah@bytereef.org>2015-01-29 13:27:23 (GMT)
committerStefan Krah <skrah@bytereef.org>2015-01-29 13:27:23 (GMT)
commitfa5d6a5ff3ca247d9c2eaf51853ff39c98c09f4a (patch)
tree1811bdf701fbeb5e9709624ede624d1bbaa9b677 /Objects/memoryobject.c
parent2934262fd36c35843c01b96657047625ce2e3cf6 (diff)
downloadcpython-fa5d6a5ff3ca247d9c2eaf51853ff39c98c09f4a.zip
cpython-fa5d6a5ff3ca247d9c2eaf51853ff39c98c09f4a.tar.gz
cpython-fa5d6a5ff3ca247d9c2eaf51853ff39c98c09f4a.tar.bz2
Issue #22668: Ensure that format strings survive slicing after casting.
Diffstat (limited to 'Objects/memoryobject.c')
-rw-r--r--Objects/memoryobject.c56
1 files changed, 52 insertions, 4 deletions
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
index cb644b8..0be8493 100644
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -1135,6 +1135,51 @@ get_native_fmtchar(char *result, const char *fmt)
return -1;
}
+Py_LOCAL_INLINE(char *)
+get_native_fmtstr(const char *fmt)
+{
+ int at = 0;
+
+ if (fmt[0] == '@') {
+ at = 1;
+ fmt++;
+ }
+ if (fmt[0] == '\0' || fmt[1] != '\0') {
+ return NULL;
+ }
+
+#define RETURN(s) do { return at ? "@" s : s; } while (0)
+
+ switch (fmt[0]) {
+ case 'c': RETURN("c");
+ case 'b': RETURN("b");
+ case 'B': RETURN("B");
+ case 'h': RETURN("h");
+ case 'H': RETURN("H");
+ case 'i': RETURN("i");
+ case 'I': RETURN("I");
+ case 'l': RETURN("l");
+ case 'L': RETURN("L");
+ #ifdef HAVE_LONG_LONG
+ case 'q': RETURN("q");
+ case 'Q': RETURN("Q");
+ #endif
+ case 'n': RETURN("n");
+ case 'N': RETURN("N");
+ case 'f': RETURN("f");
+ case 'd': RETURN("d");
+ #ifdef HAVE_C99_BOOL
+ case '?': RETURN("?");
+ #else
+ case '?': RETURN("?");
+ #endif
+ case 'P': RETURN("P");
+ }
+
+ return NULL;
+}
+
+
/* Cast a memoryview's data type to 'format'. The input array must be
C-contiguous. At least one of input-format, output-format must have
byte size. The output array is 1-D, with the same byte length as the
@@ -1184,10 +1229,13 @@ cast_to_1D(PyMemoryViewObject *mv, PyObject *format)
goto out;
}
- strncpy(mv->format, PyBytes_AS_STRING(asciifmt),
- _Py_MEMORYVIEW_MAX_FORMAT);
- mv->format[_Py_MEMORYVIEW_MAX_FORMAT-1] = '\0';
- view->format = mv->format;
+ view->format = get_native_fmtstr(PyBytes_AS_STRING(asciifmt));
+ if (view->format == NULL) {
+ /* NOT_REACHED: get_native_fmtchar() already validates the format. */
+ PyErr_SetString(PyExc_RuntimeError,
+ "memoryview: internal error");
+ goto out;
+ }
view->itemsize = itemsize;
view->ndim = 1;