Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(12)

Side by Side Diff: Modules/arraymodule.c

Issue 2599: python - fix buffer overflows in unicode processing and elsewhere SVN Base: http://svn.python.org/view/*checkout*/python/trunk/
Patch Set: Created 1 year, 4 months ago , Downloaded from: http://bugs.python.org/file10825/issue2620-gps02-patch.txt
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* Array object implementation */ 1 /* Array object implementation */
2 2
3 /* An array is a uniform list -- all items have the same type. 3 /* An array is a uniform list -- all items have the same type.
4 The item type is restricted to simple C types like int or float */ 4 The item type is restricted to simple C types like int or float */
5 5
6 #define PY_SSIZE_T_CLEAN 6 #define PY_SSIZE_T_CLEAN
7 #include "Python.h" 7 #include "Python.h"
8 #include "structmember.h" 8 #include "structmember.h"
9 9
10 #ifdef STDC_HEADERS 10 #ifdef STDC_HEADERS
(...skipping 797 matching lines...) Expand 10 before | Expand all | Expand 10 after
808 Py_DECREF(it); 808 Py_DECREF(it);
809 if (PyErr_Occurred()) 809 if (PyErr_Occurred())
810 return -1; 810 return -1;
811 return 0; 811 return 0;
812 } 812 }
813 813
814 static int 814 static int
815 array_do_extend(arrayobject *self, PyObject *bb) 815 array_do_extend(arrayobject *self, PyObject *bb)
816 { 816 {
817 Py_ssize_t size; 817 Py_ssize_t size;
818 char *old_item;
818 819
819 if (!array_Check(bb)) 820 if (!array_Check(bb))
820 return array_iter_extend(self, bb); 821 return array_iter_extend(self, bb);
821 #define b ((arrayobject *)bb) 822 #define b ((arrayobject *)bb)
822 if (self->ob_descr != b->ob_descr) { 823 if (self->ob_descr != b->ob_descr) {
823 PyErr_SetString(PyExc_TypeError, 824 PyErr_SetString(PyExc_TypeError,
824 "can only extend with array of same kind"); 825 "can only extend with array of same kind");
825 return -1; 826 return -1;
826 } 827 }
827 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) || 828 if ((Py_SIZE(self) > PY_SSIZE_T_MAX - Py_SIZE(b)) ||
828 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr- >itemsize)) { 829 ((Py_SIZE(self) + Py_SIZE(b)) > PY_SSIZE_T_MAX / self->ob_descr- >itemsize)) {
829 PyErr_NoMemory(); 830 PyErr_NoMemory();
830 return -1; 831 return -1;
831 } 832 }
832 size = Py_SIZE(self) + Py_SIZE(b); 833 size = Py_SIZE(self) + Py_SIZE(b);
834 old_item = self->ob_item;
833 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize); 835 PyMem_RESIZE(self->ob_item, char, size*self->ob_descr->itemsize);
834 if (self->ob_item == NULL) { 836 if (self->ob_item == NULL) {
837 self->ob_item = old_item;
835 PyErr_NoMemory(); 838 PyErr_NoMemory();
836 return -1; 839 return -1;
837 } 840 }
838 memcpy(self->ob_item + Py_SIZE(self)*self->ob_descr->itemsize, 841 memcpy(self->ob_item + Py_SIZE(self)*self->ob_descr->itemsize,
839 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize); 842 b->ob_item, Py_SIZE(b)*b->ob_descr->itemsize);
840 Py_SIZE(self) = size; 843 Py_SIZE(self) = size;
841 self->allocated = size; 844 self->allocated = size;
842 845
843 return 0; 846 return 0;
844 #undef b 847 #undef b
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
877 if (n == 0) { 880 if (n == 0) {
878 PyMem_FREE(items); 881 PyMem_FREE(items);
879 self->ob_item = NULL; 882 self->ob_item = NULL;
880 Py_SIZE(self) = 0; 883 Py_SIZE(self) = 0;
881 self->allocated = 0; 884 self->allocated = 0;
882 } 885 }
883 else { 886 else {
884 if (size > PY_SSIZE_T_MAX / n) { 887 if (size > PY_SSIZE_T_MAX / n) {
885 return PyErr_NoMemory(); 888 return PyErr_NoMemory();
886 } 889 }
887 » » » PyMem_Resize(items, char, n * size); 890 » » » PyMem_RESIZE(items, char, n * size);
888 if (items == NULL) 891 if (items == NULL)
889 return PyErr_NoMemory(); 892 return PyErr_NoMemory();
890 p = items; 893 p = items;
891 for (i = 1; i < n; i++) { 894 for (i = 1; i < n; i++) {
892 p += size; 895 p += size;
893 memcpy(p, items, size); 896 memcpy(p, items, size);
894 } 897 }
895 self->ob_item = items; 898 self->ob_item = items;
896 Py_SIZE(self) *= n; 899 Py_SIZE(self) *= n;
897 self->allocated = Py_SIZE(self); 900 self->allocated = Py_SIZE(self);
(...skipping 1331 matching lines...) Expand 10 before | Expand all | Expand 10 after
2229 m = Py_InitModule3("array", a_methods, module_doc); 2232 m = Py_InitModule3("array", a_methods, module_doc);
2230 if (m == NULL) 2233 if (m == NULL)
2231 return; 2234 return;
2232 2235
2233 Py_INCREF((PyObject *)&Arraytype); 2236 Py_INCREF((PyObject *)&Arraytype);
2234 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype); 2237 PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
2235 Py_INCREF((PyObject *)&Arraytype); 2238 Py_INCREF((PyObject *)&Arraytype);
2236 PyModule_AddObject(m, "array", (PyObject *)&Arraytype); 2239 PyModule_AddObject(m, "array", (PyObject *)&Arraytype);
2237 /* No need to check the error here, the caller will do that */ 2240 /* No need to check the error here, the caller will do that */
2238 } 2241 }
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r497