A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/python/cpython/commit/44a93e54f4b0f90634d16d53c437fabb6946ea9d below:

Fix the overflows in expandtabs(). "This time for sure!" · python/cpython@44a93e5 · GitHub

File tree Expand file treeCollapse file tree 2 files changed

+65

-50

lines changed

Filter options

Expand file treeCollapse file tree 2 files changed

+65

-50

lines changed Original file line number Diff line number Diff line change

@@ -3299,73 +3299,80 @@ If tabsize is not given, a tab size of 8 characters is assumed.");

3299 3299

static PyObject*

3300 3300

string_expandtabs(PyStringObject *self, PyObject *args)

3301 3301

{

3302 -

const char *e, *p;

3302 +

const char *e, *p, *qe;

3303 3303

char *q;

3304 -

Py_ssize_t i, j, old_j;

3304 +

Py_ssize_t i, j, incr;

3305 3305

PyObject *u;

3306 3306

int tabsize = 8;

3307 3307 3308 3308

if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))

3309 3309

return NULL;

3310 3310 3311 3311

/* First pass: determine size of output string */

3312 -

i = j = old_j = 0;

3313 -

e = PyString_AS_STRING(self) + PyString_GET_SIZE(self);

3312 +

i = 0; /* chars up to and including most recent \n or \r */

3313 +

j = 0; /* chars since most recent \n or \r (use in tab calculations) */

3314 +

e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); /* end of input */

3314 3315

for (p = PyString_AS_STRING(self); p < e; p++)

3315 3316

if (*p == '\t') {

3316 3317

if (tabsize > 0) {

3317 -

j += tabsize - (j % tabsize);

3318 -

if (old_j > j) {

3319 -

PyErr_SetString(PyExc_OverflowError,

3320 -

"new string is too long");

3321 -

return NULL;

3322 -

}

3323 -

old_j = j;

3318 +

incr = tabsize - (j % tabsize);

3319 +

if (j > PY_SSIZE_T_MAX - incr)

3320 +

goto overflow1;

3321 +

j += incr;

3324 3322

}

3325 3323

}

3326 3324

else {

3325 +

if (j > PY_SSIZE_T_MAX - 1)

3326 +

goto overflow1;

3327 3327

j++;

3328 3328

if (*p == '\n' || *p == '\r') {

3329 +

if (i > PY_SSIZE_T_MAX - j)

3330 +

goto overflow1;

3329 3331

i += j;

3330 -

old_j = j = 0;

3331 -

if (i < 0) {

3332 -

PyErr_SetString(PyExc_OverflowError,

3333 -

"new string is too long");

3334 -

return NULL;

3335 -

}

3332 +

j = 0;

3336 3333

}

3337 3334

}

3338 3335 3339 -

if ((i + j) < 0) {

3340 -

PyErr_SetString(PyExc_OverflowError, "new string is too long");

3341 -

return NULL;

3342 -

}

3336 +

if (i > PY_SSIZE_T_MAX - j)

3337 +

goto overflow1;

3343 3338 3344 3339

/* Second pass: create output string and fill it */

3345 3340

u = PyString_FromStringAndSize(NULL, i + j);

3346 3341

if (!u)

3347 3342

return NULL;

3348 3343 3349 -

j = 0;

3350 -

q = PyString_AS_STRING(u);

3344 +

j = 0; /* same as in first pass */

3345 +

q = PyString_AS_STRING(u); /* next output char */

3346 +

qe = PyString_AS_STRING(u) + PyString_GET_SIZE(u); /* end of output */

3351 3347 3352 3348

for (p = PyString_AS_STRING(self); p < e; p++)

3353 3349

if (*p == '\t') {

3354 3350

if (tabsize > 0) {

3355 3351

i = tabsize - (j % tabsize);

3356 3352

j += i;

3357 -

while (i--)

3353 +

while (i--) {

3354 +

if (q >= qe)

3355 +

goto overflow2;

3358 3356

*q++ = ' ';

3357 +

}

3359 3358

}

3360 3359

}

3361 3360

else {

3362 -

j++;

3361 +

if (q >= qe)

3362 +

goto overflow2;

3363 3363

*q++ = *p;

3364 +

j++;

3364 3365

if (*p == '\n' || *p == '\r')

3365 3366

j = 0;

3366 3367

}

3367 3368 3368 3369

return u;

3370 + 3371 +

overflow2:

3372 +

Py_DECREF(u);

3373 +

overflow1:

3374 +

PyErr_SetString(PyExc_OverflowError, "new string is too long");

3375 +

return NULL;

3369 3376

}

3370 3377 3371 3378

Py_LOCAL_INLINE(PyObject *)

Original file line number Diff line number Diff line change

@@ -5689,71 +5689,79 @@ unicode_expandtabs(PyUnicodeObject *self, PyObject *args)

5689 5689

Py_UNICODE *e;

5690 5690

Py_UNICODE *p;

5691 5691

Py_UNICODE *q;

5692 -

Py_ssize_t i, j, old_j;

5692 +

Py_UNICODE *qe;

5693 +

Py_ssize_t i, j, incr;

5693 5694

PyUnicodeObject *u;

5694 5695

int tabsize = 8;

5695 5696 5696 5697

if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))

5697 5698

return NULL;

5698 5699 5699 5700

/* First pass: determine size of output string */

5700 -

i = j = old_j = 0;

5701 -

e = self->str + self->length;

5701 +

i = 0; /* chars up to and including most recent \n or \r */

5702 +

j = 0; /* chars since most recent \n or \r (use in tab calculations) */

5703 +

e = self->str + self->length; /* end of input */

5702 5704

for (p = self->str; p < e; p++)

5703 5705

if (*p == '\t') {

5704 5706

if (tabsize > 0) {

5705 -

j += tabsize - (j % tabsize);

5706 -

if (old_j > j) {

5707 -

PyErr_SetString(PyExc_OverflowError,

5708 -

"new string is too long");

5709 -

return NULL;

5710 -

}

5711 -

old_j = j;

5712 -

}

5707 +

incr = tabsize - (j % tabsize); /* cannot overflow */

5708 +

if (j > PY_SSIZE_T_MAX - incr)

5709 +

goto overflow1;

5710 +

j += incr;

5711 +

}

5713 5712

}

5714 5713

else {

5714 +

if (j > PY_SSIZE_T_MAX - 1)

5715 +

goto overflow1;

5715 5716

j++;

5716 5717

if (*p == '\n' || *p == '\r') {

5718 +

if (i > PY_SSIZE_T_MAX - j)

5719 +

goto overflow1;

5717 5720

i += j;

5718 -

old_j = j = 0;

5719 -

if (i < 0) {

5720 -

PyErr_SetString(PyExc_OverflowError,

5721 -

"new string is too long");

5722 -

return NULL;

5723 -

}

5721 +

j = 0;

5724 5722

}

5725 5723

}

5726 5724 5727 -

if ((i + j) < 0) {

5728 -

PyErr_SetString(PyExc_OverflowError, "new string is too long");

5729 -

return NULL;

5730 -

}

5725 +

if (i > PY_SSIZE_T_MAX - j)

5726 +

goto overflow1;

5731 5727 5732 5728

/* Second pass: create output string and fill it */

5733 5729

u = _PyUnicode_New(i + j);

5734 5730

if (!u)

5735 5731

return NULL;

5736 5732 5737 -

j = 0;

5738 -

q = u->str;

5733 +

j = 0; /* same as in first pass */

5734 +

q = u->str; /* next output char */

5735 +

qe = u->str + u->length; /* end of output */

5739 5736 5740 5737

for (p = self->str; p < e; p++)

5741 5738

if (*p == '\t') {

5742 5739

if (tabsize > 0) {

5743 5740

i = tabsize - (j % tabsize);

5744 5741

j += i;

5745 -

while (i--)

5742 +

while (i--) {

5743 +

if (q >= qe)

5744 +

goto overflow2;

5746 5745

*q++ = ' ';

5746 +

}

5747 5747

}

5748 5748

}

5749 5749

else {

5750 -

j++;

5750 +

if (q >= qe)

5751 +

goto overflow2;

5751 5752

*q++ = *p;

5753 +

j++;

5752 5754

if (*p == '\n' || *p == '\r')

5753 5755

j = 0;

5754 5756

}

5755 5757 5756 5758

return (PyObject*) u;

5759 + 5760 +

overflow2:

5761 +

Py_DECREF(u);

5762 +

overflow1:

5763 +

PyErr_SetString(PyExc_OverflowError, "new string is too long");

5764 +

return NULL;

5757 5765

}

5758 5766 5759 5767

PyDoc_STRVAR(find__doc__,

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4