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

Delta Between Two Patch Sets: Objects/bytearrayobject.c

Issue 24467: bytearray pop and remove Buffer Over-read
Left Patch Set: Created 4 years, 7 months ago
Right Patch Set: Created 4 years, 7 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_bytes.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 #include "bytesobject.h" 7 #include "bytesobject.h"
8 #include "pystrhex.h" 8 #include "pystrhex.h"
9 9
10 /*[clinic input] 10 /*[clinic input]
(...skipping 873 matching lines...) Expand 10 before | Expand all | Expand 10 after
884 break; 884 break;
885 } 885 }
886 886
887 /* Interpret it as an int (__index__) */ 887 /* Interpret it as an int (__index__) */
888 rc = _getbytevalue(item, &value); 888 rc = _getbytevalue(item, &value);
889 Py_DECREF(item); 889 Py_DECREF(item);
890 if (!rc) 890 if (!rc)
891 goto error; 891 goto error;
892 892
893 /* Append the byte */ 893 /* Append the byte */
894 if (Py_SIZE(self) < self->ob_alloc) 894 if (Py_SIZE(self) + 1 < self->ob_alloc) {
895 Py_SIZE(self)++; 895 Py_SIZE(self)++;
896 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
897 }
896 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) 898 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
897 goto error; 899 goto error;
898 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value; 900 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
899 } 901 }
900 902
901 /* Clean up and return success */ 903 /* Clean up and return success */
902 Py_DECREF(it); 904 Py_DECREF(it);
903 return 0; 905 return 0;
904 906
905 error: 907 error:
(...skipping 1614 matching lines...) Expand 10 before | Expand all | Expand 10 after
2520 int value; 2522 int value;
2521 Py_ssize_t n = Py_SIZE(self); 2523 Py_ssize_t n = Py_SIZE(self);
2522 char *buf; 2524 char *buf;
2523 2525
2524 if (n == 0) { 2526 if (n == 0) {
2525 PyErr_SetString(PyExc_IndexError, 2527 PyErr_SetString(PyExc_IndexError,
2526 "pop from empty bytearray"); 2528 "pop from empty bytearray");
2527 return NULL; 2529 return NULL;
2528 } 2530 }
2529 if (index < 0) 2531 if (index < 0)
2530 index += n; 2532 index += Py_SIZE(self);
2531 if (index < 0 || index >= n) { 2533 if (index < 0 || index >= Py_SIZE(self)) {
2532 PyErr_SetString(PyExc_IndexError, "pop index out of range"); 2534 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2533 return NULL; 2535 return NULL;
2534 } 2536 }
2535 if (!_canresize(self)) 2537 if (!_canresize(self))
2536 return NULL; 2538 return NULL;
2537 2539
2538 buf = PyByteArray_AS_STRING(self); 2540 buf = PyByteArray_AS_STRING(self);
2539 value = buf[index]; 2541 value = buf[index];
2540 memmove(buf + index, buf + index + 1, n - index - 1); 2542 memmove(buf + index, buf + index + 1, n - index);
2541 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) 2543 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2542 return NULL; 2544 return NULL;
2543 2545
2544 return PyLong_FromLong((unsigned char)value); 2546 return PyLong_FromLong((unsigned char)value);
2545 } 2547 }
2546 2548
2547 /*[clinic input] 2549 /*[clinic input]
2548 bytearray.remove 2550 bytearray.remove
2549 2551
2550 self: self(type="PyByteArrayObject *") 2552 self: self(type="PyByteArrayObject *")
(...skipping 15 matching lines...) Expand all
2566 if (buf[where] == value) 2568 if (buf[where] == value)
2567 break; 2569 break;
2568 } 2570 }
2569 if (where == n) { 2571 if (where == n) {
2570 PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); 2572 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
2571 return NULL; 2573 return NULL;
2572 } 2574 }
2573 if (!_canresize(self)) 2575 if (!_canresize(self))
2574 return NULL; 2576 return NULL;
2575 2577
2576 memmove(buf + where, buf + where + 1, n - where - 1); 2578 memmove(buf + where, buf + where + 1, n - where);
2577 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) 2579 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2578 return NULL; 2580 return NULL;
2579 2581
2580 Py_RETURN_NONE; 2582 Py_RETURN_NONE;
2581 } 2583 }
2582 2584
2583 /* XXX These two helpers could be optimized if argsize == 1 */ 2585 /* XXX These two helpers could be optimized if argsize == 1 */
2584 2586
2585 static Py_ssize_t 2587 static Py_ssize_t
2586 lstrip_helper(char *myptr, Py_ssize_t mysize, 2588 lstrip_helper(char *myptr, Py_ssize_t mysize,
(...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after
3283 } 3285 }
3284 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); 3286 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3285 if (it == NULL) 3287 if (it == NULL)
3286 return NULL; 3288 return NULL;
3287 it->it_index = 0; 3289 it->it_index = 0;
3288 Py_INCREF(seq); 3290 Py_INCREF(seq);
3289 it->it_seq = (PyByteArrayObject *)seq; 3291 it->it_seq = (PyByteArrayObject *)seq;
3290 _PyObject_GC_TRACK(it); 3292 _PyObject_GC_TRACK(it);
3291 return (PyObject *)it; 3293 return (PyObject *)it;
3292 } 3294 }
LEFTRIGHT

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