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

Side by Side Diff: Objects/bytearrayobject.c

Issue 24467: bytearray pop and remove Buffer Over-read
Patch Set: Created 4 years, 8 months ago
Left:
Right:
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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* PyByteArray (bytearray) implementation */ 1 /* PyByteArray (bytearray) implementation */
2 2
3 #define PY_SSIZE_T_CLEAN 3 #define PY_SSIZE_T_CLEAN
4 #include "Python.h" 4 #include "Python.h"
5 #include "structmember.h" 5 #include "structmember.h"
6 #include "bytes_methods.h" 6 #include "bytes_methods.h"
7 7
8 char _PyByteArray_empty_string[] = ""; 8 char _PyByteArray_empty_string[] = "";
9 9
10 void 10 void
(...skipping 2345 matching lines...) Expand 10 before | Expand all | Expand 10 after
2356 2356
2357 if (!PyArg_ParseTuple(args, "|n:pop", &where)) 2357 if (!PyArg_ParseTuple(args, "|n:pop", &where))
2358 return NULL; 2358 return NULL;
2359 2359
2360 if (n == 0) { 2360 if (n == 0) {
2361 PyErr_SetString(PyExc_IndexError, 2361 PyErr_SetString(PyExc_IndexError,
2362 "pop from empty bytearray"); 2362 "pop from empty bytearray");
2363 return NULL; 2363 return NULL;
2364 } 2364 }
2365 if (where < 0) 2365 if (where < 0)
2366 where += Py_SIZE(self); 2366 where += n;
2367 if (where < 0 || where >= Py_SIZE(self)) { 2367 if (where < 0 || where >= n) {
2368 PyErr_SetString(PyExc_IndexError, "pop index out of range"); 2368 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2369 return NULL; 2369 return NULL;
2370 } 2370 }
2371 if (!_canresize(self)) 2371 if (!_canresize(self))
2372 return NULL; 2372 return NULL;
2373 2373
2374 value = self->ob_bytes[where]; 2374 value = self->ob_bytes[where];
2375 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); 2375 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where - 1);
2376 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) 2376 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2377 return NULL; 2377 return NULL;
2378 2378
2379 return PyLong_FromLong((unsigned char)value); 2379 return PyLong_FromLong((unsigned char)value);
2380 } 2380 }
2381 2381
2382 PyDoc_STRVAR(remove__doc__, 2382 PyDoc_STRVAR(remove__doc__,
2383 "B.remove(int) -> None\n\ 2383 "B.remove(int) -> None\n\
2384 \n\ 2384 \n\
2385 Remove the first occurrence of a value in B."); 2385 Remove the first occurrence of a value in B.");
(...skipping 10 matching lines...) Expand all
2396 if (self->ob_bytes[where] == value) 2396 if (self->ob_bytes[where] == value)
2397 break; 2397 break;
2398 } 2398 }
2399 if (where == n) { 2399 if (where == n) {
2400 PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); 2400 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
2401 return NULL; 2401 return NULL;
2402 } 2402 }
2403 if (!_canresize(self)) 2403 if (!_canresize(self))
2404 return NULL; 2404 return NULL;
2405 2405
2406 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where); 2406 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where - 1);
2407 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) 2407 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2408 return NULL; 2408 return NULL;
2409 2409
2410 Py_RETURN_NONE; 2410 Py_RETURN_NONE;
2411 } 2411 }
2412 2412
2413 /* XXX These two helpers could be optimized if argsize == 1 */ 2413 /* XXX These two helpers could be optimized if argsize == 1 */
2414 2414
2415 static Py_ssize_t 2415 static Py_ssize_t
2416 lstrip_helper(unsigned char *myptr, Py_ssize_t mysize, 2416 lstrip_helper(unsigned char *myptr, Py_ssize_t mysize,
(...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after
3109 } 3109 }
3110 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); 3110 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3111 if (it == NULL) 3111 if (it == NULL)
3112 return NULL; 3112 return NULL;
3113 it->it_index = 0; 3113 it->it_index = 0;
3114 Py_INCREF(seq); 3114 Py_INCREF(seq);
3115 it->it_seq = (PyByteArrayObject *)seq; 3115 it->it_seq = (PyByteArrayObject *)seq;
3116 _PyObject_GC_TRACK(it); 3116 _PyObject_GC_TRACK(it);
3117 return (PyObject *)it; 3117 return (PyObject *)it;
3118 } 3118 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+