Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 230cae7

Browse files
committed
Trent Mick <[email protected]>:
Limit the 'b' formatter of PyArg_ParseTuple to valid values of an unsigned char, i.e. [0,UCHAR_MAX]. It is expected that this is the common usage of 'b'. An OverflowError is raised if the parsed value is outside this range.
1 parent a69ef82 commit 230cae7

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

Python/getargs.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -465,62 +465,62 @@ convertsimple1(arg, p_format, p_va)
465465

466466
switch (c) {
467467

468-
case 'b': /* byte -- very short int */
468+
case 'b': /* unsigned byte -- very short int */
469469
{
470470
char *p = va_arg(*p_va, char *);
471471
long ival = PyInt_AsLong(arg);
472472
if (ival == -1 && PyErr_Occurred())
473473
return "integer<b>";
474-
else if (ival < CHAR_MIN) {
474+
else if (ival < 0) {
475475
PyErr_SetString(PyExc_OverflowError,
476-
"byte integer is less than minimum");
476+
"unsigned byte integer is less than minimum");
477477
return "integer<b>";
478478
}
479-
else if (ival > CHAR_MAX && ival >= 256) {
479+
else if (ival > UCHAR_MAX) {
480480
PyErr_SetString(PyExc_OverflowError,
481-
"byte integer is greater than maximum");
481+
"unsigned byte integer is greater than maximum");
482482
return "integer<b>";
483483
}
484484
else
485-
*p = (char) ival;
485+
*p = (unsigned char) ival;
486486
break;
487487
}
488488

489-
case 'h': /* short int */
489+
case 'h': /* signed short int */
490490
{
491491
short *p = va_arg(*p_va, short *);
492492
long ival = PyInt_AsLong(arg);
493493
if (ival == -1 && PyErr_Occurred())
494494
return "integer<h>";
495495
else if (ival < SHRT_MIN) {
496496
PyErr_SetString(PyExc_OverflowError,
497-
"short integer is less than minimum");
497+
"signed short integer is less than minimum");
498498
return "integer<h>";
499499
}
500500
else if (ival > SHRT_MAX) {
501501
PyErr_SetString(PyExc_OverflowError,
502-
"short integer is greater than maximum");
502+
"signed short integer is greater than maximum");
503503
return "integer<h>";
504504
}
505505
else
506506
*p = (short) ival;
507507
break;
508508
}
509509

510-
case 'i': /* int */
510+
case 'i': /* signed int */
511511
{
512512
int *p = va_arg(*p_va, int *);
513513
long ival = PyInt_AsLong(arg);
514514
if (ival == -1 && PyErr_Occurred())
515515
return "integer<i>";
516516
else if (ival < INT_MIN) {
517517
PyErr_SetString(PyExc_OverflowError,
518-
"integer is less than minimum");
518+
"signed integer is less than minimum");
519519
return "integer<i>";
520520
}
521521
else if (ival > INT_MAX) {
522522
PyErr_SetString(PyExc_OverflowError,
523-
"integer is greater than maximum");
523+
"signed integer is greater than maximum");
524524
return "integer<i>";
525525
}
526526
else

0 commit comments

Comments
 (0)