@@ -527,12 +527,15 @@ static int
527
527
np_byte (_structmodulestate * state , char * p , PyObject * v , const formatdef * f )
528
528
{
529
529
long x ;
530
- if (get_long (state , v , & x ) < 0 || x < -128 || x > 127 ) {
531
- if (! PyErr_ExceptionMatches (state -> StructError )) {
530
+ if (get_long (state , v , & x ) < 0 ) {
531
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
532
532
RANGE_ERROR (state , f , 0 );
533
533
}
534
534
return -1 ;
535
535
}
536
+ if (x < -128 || x > 127 ) {
537
+ RANGE_ERROR (state , f , 0 );
538
+ }
536
539
* p = (char )x ;
537
540
return 0 ;
538
541
}
@@ -541,12 +544,15 @@ static int
541
544
np_ubyte (_structmodulestate * state , char * p , PyObject * v , const formatdef * f )
542
545
{
543
546
long x ;
544
- if (get_long (state , v , & x ) < 0 || x < 0 || x > 255 ) {
545
- if (! PyErr_ExceptionMatches (state -> StructError )) {
547
+ if (get_long (state , v , & x ) < 0 ) {
548
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
546
549
RANGE_ERROR (state , f , 1 );
547
550
}
548
551
return -1 ;
549
552
}
553
+ if (x < 0 || x > 255 ) {
554
+ RANGE_ERROR (state , f , 1 );
555
+ }
550
556
* (unsigned char * )p = (unsigned char )x ;
551
557
return 0 ;
552
558
}
@@ -568,12 +574,15 @@ np_short(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
568
574
{
569
575
long x ;
570
576
short y ;
571
- if (get_long (state , v , & x ) < 0 || x < SHRT_MIN || x > SHRT_MAX ) {
572
- if (! PyErr_ExceptionMatches (state -> StructError )) {
577
+ if (get_long (state , v , & x ) < 0 ) {
578
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
573
579
RANGE_ERROR (state , f , 0 );
574
580
}
575
581
return -1 ;
576
582
}
583
+ if (x < SHRT_MIN || x > SHRT_MAX ) {
584
+ RANGE_ERROR (state , f , 0 );
585
+ }
577
586
y = (short )x ;
578
587
memcpy (p , (char * )& y , sizeof y );
579
588
return 0 ;
@@ -584,12 +593,15 @@ np_ushort(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
584
593
{
585
594
long x ;
586
595
unsigned short y ;
587
- if (get_long (state , v , & x ) < 0 || x < 0 || x > USHRT_MAX ) {
588
- if (! PyErr_ExceptionMatches (state -> StructError )) {
596
+ if (get_long (state , v , & x ) < 0 ) {
597
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
589
598
RANGE_ERROR (state , f , 1 );
590
599
}
591
600
return -1 ;
592
601
}
602
+ if (x < 0 || x > USHRT_MAX ) {
603
+ RANGE_ERROR (state , f , 1 );
604
+ }
593
605
y = (unsigned short )x ;
594
606
memcpy (p , (char * )& y , sizeof y );
595
607
return 0 ;
@@ -601,7 +613,7 @@ np_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
601
613
long x ;
602
614
int y ;
603
615
if (get_long (state , v , & x ) < 0 ) {
604
- if (! PyErr_ExceptionMatches (state -> StructError )) {
616
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
605
617
RANGE_ERROR (state , f , 0 );
606
618
}
607
619
return -1 ;
@@ -621,7 +633,7 @@ np_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
621
633
unsigned long x ;
622
634
unsigned int y ;
623
635
if (get_ulong (state , v , & x ) < 0 ) {
624
- if (! PyErr_ExceptionMatches (state -> StructError )) {
636
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
625
637
RANGE_ERROR (state , f , 1 );
626
638
}
627
639
return -1 ;
@@ -640,7 +652,7 @@ np_long(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
640
652
{
641
653
long x ;
642
654
if (get_long (state , v , & x ) < 0 ) {
643
- if (! PyErr_ExceptionMatches (state -> StructError )) {
655
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
644
656
RANGE_ERROR (state , f , 0 );
645
657
}
646
658
return -1 ;
@@ -654,7 +666,7 @@ np_ulong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
654
666
{
655
667
unsigned long x ;
656
668
if (get_ulong (state , v , & x ) < 0 ) {
657
- if (! PyErr_ExceptionMatches (state -> StructError )) {
669
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
658
670
RANGE_ERROR (state , f , 1 );
659
671
}
660
672
return -1 ;
@@ -668,7 +680,7 @@ np_ssize_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
668
680
{
669
681
Py_ssize_t x ;
670
682
if (get_ssize_t (state , v , & x ) < 0 ) {
671
- if (! PyErr_ExceptionMatches (state -> StructError )) {
683
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
672
684
RANGE_ERROR (state , f , 0 );
673
685
}
674
686
return -1 ;
@@ -682,7 +694,7 @@ np_size_t(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
682
694
{
683
695
size_t x ;
684
696
if (get_size_t (state , v , & x ) < 0 ) {
685
- if (! PyErr_ExceptionMatches (state -> StructError )) {
697
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
686
698
RANGE_ERROR (state , f , 1 );
687
699
}
688
700
return -1 ;
@@ -696,7 +708,7 @@ np_longlong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
696
708
{
697
709
long long x ;
698
710
if (get_longlong (state , v , & x ) < 0 ) {
699
- if (! PyErr_ExceptionMatches (state -> StructError )) {
711
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
700
712
PyErr_Format (state -> StructError ,
701
713
"'%c' format requires %lld <= number <= %lld" ,
702
714
f -> format ,
@@ -714,7 +726,7 @@ np_ulonglong(_structmodulestate *state, char *p, PyObject *v, const formatdef *f
714
726
{
715
727
unsigned long long x ;
716
728
if (get_ulonglong (state , v , & x ) < 0 ) {
717
- if (! PyErr_ExceptionMatches (state -> StructError )) {
729
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
718
730
PyErr_Format (state -> StructError ,
719
731
"'%c' format requires 0 <= number <= %llu" ,
720
732
f -> format ,
@@ -927,7 +939,7 @@ bp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
927
939
Py_ssize_t i ;
928
940
unsigned char * q = (unsigned char * )p ;
929
941
if (get_long (state , v , & x ) < 0 ) {
930
- if (! PyErr_ExceptionMatches (state -> StructError )) {
942
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
931
943
RANGE_ERROR (state , f , 0 );
932
944
}
933
945
return -1 ;
@@ -955,7 +967,7 @@ bp_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
955
967
Py_ssize_t i ;
956
968
unsigned char * q = (unsigned char * )p ;
957
969
if (get_ulong (state , v , & x ) < 0 ) {
958
- if (! PyErr_ExceptionMatches (state -> StructError )) {
970
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
959
971
RANGE_ERROR (state , f , 1 );
960
972
}
961
973
return -1 ;
@@ -1187,7 +1199,7 @@ lp_int(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
1187
1199
Py_ssize_t i ;
1188
1200
unsigned char * q = (unsigned char * )p ;
1189
1201
if (get_long (state , v , & x ) < 0 ) {
1190
- if (! PyErr_ExceptionMatches (state -> StructError )) {
1202
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
1191
1203
RANGE_ERROR (state , f , 0 );
1192
1204
}
1193
1205
return -1 ;
@@ -1215,7 +1227,7 @@ lp_uint(_structmodulestate *state, char *p, PyObject *v, const formatdef *f)
1215
1227
Py_ssize_t i ;
1216
1228
unsigned char * q = (unsigned char * )p ;
1217
1229
if (get_ulong (state , v , & x ) < 0 ) {
1218
- if (! PyErr_ExceptionMatches (state -> StructError )) {
1230
+ if (PyErr_ExceptionMatches (PyExc_OverflowError )) {
1219
1231
RANGE_ERROR (state , f , 1 );
1220
1232
}
1221
1233
return -1 ;
0 commit comments