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

Skip to content

Commit 1fa174a

Browse files
committed
Get rid of signed/unsigned comparaison in _sre.c
Fix compilation warnings on Windows (Visual C++) like: "_sre.c(3121): warning C4018: '>' : signed/unsigned mismatch". _validate_outer() ensures that groups >= 0, so _validate_inner() can cast groups to size_t.
1 parent 36a5a06 commit 1fa174a

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

Modules/_sre.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
795795
if (ctx->pattern[0] == SRE_OP_INFO) {
796796
/* optimization info block */
797797
/* <INFO> <1=skip> <2=flags> <3=min> ... */
798-
if (ctx->pattern[3] && (end - ctx->ptr)/state->charsize < ctx->pattern[3]) {
798+
if (ctx->pattern[3] && (Py_uintptr_t)(end - ctx->ptr)/state->charsize < ctx->pattern[3]) {
799799
TRACE(("reject (got %d chars, need %d)\n",
800800
(end - ctx->ptr)/state->charsize, ctx->pattern[3]));
801801
RETURN_FAILURE;
@@ -2762,7 +2762,7 @@ _compile(PyObject* self_, PyObject* args)
27622762
skip = *code; \
27632763
VTRACE(("%lu (skip to %p)\n", \
27642764
(unsigned long)skip, code+skip)); \
2765-
if (skip-adj > end-code) \
2765+
if (skip-adj > (Py_uintptr_t)(end - code)) \
27662766
FAIL; \
27672767
code++; \
27682768
} while (0)
@@ -2795,15 +2795,15 @@ _validate_charset(SRE_CODE *code, SRE_CODE *end)
27952795

27962796
case SRE_OP_CHARSET:
27972797
offset = 32/sizeof(SRE_CODE); /* 32-byte bitmap */
2798-
if (offset > end-code)
2798+
if (offset > (Py_uintptr_t)(end - code))
27992799
FAIL;
28002800
code += offset;
28012801
break;
28022802

28032803
case SRE_OP_BIGCHARSET:
28042804
GET_ARG; /* Number of blocks */
28052805
offset = 256/sizeof(SRE_CODE); /* 256-byte table */
2806-
if (offset > end-code)
2806+
if (offset > (Py_uintptr_t)(end - code))
28072807
FAIL;
28082808
/* Make sure that each byte points to a valid block */
28092809
for (i = 0; i < 256; i++) {
@@ -2812,7 +2812,7 @@ _validate_charset(SRE_CODE *code, SRE_CODE *end)
28122812
}
28132813
code += offset;
28142814
offset = arg * 32/sizeof(SRE_CODE); /* 32-byte bitmap times arg */
2815-
if (offset > end-code)
2815+
if (offset > (Py_uintptr_t)(end - code))
28162816
FAIL;
28172817
code += offset;
28182818
break;
@@ -2875,7 +2875,7 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
28752875
sre_match() code is robust even if they don't, and the worst
28762876
you can get is nonsensical match results. */
28772877
GET_ARG;
2878-
if (arg > 2*groups+1) {
2878+
if (arg > 2 * (size_t)groups + 1) {
28792879
VTRACE(("arg=%d, groups=%d\n", (int)arg, (int)groups));
28802880
FAIL;
28812881
}
@@ -2963,11 +2963,11 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
29632963
GET_ARG; prefix_len = arg;
29642964
GET_ARG;
29652965
/* Here comes the prefix string */
2966-
if (prefix_len > newcode-code)
2966+
if (prefix_len > (Py_uintptr_t)(newcode - code))
29672967
FAIL;
29682968
code += prefix_len;
29692969
/* And here comes the overlap table */
2970-
if (prefix_len > newcode-code)
2970+
if (prefix_len > (Py_uintptr_t)(newcode - code))
29712971
FAIL;
29722972
/* Each overlap value should be < prefix_len */
29732973
for (i = 0; i < prefix_len; i++) {
@@ -3058,7 +3058,7 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
30583058
case SRE_OP_GROUPREF:
30593059
case SRE_OP_GROUPREF_IGNORE:
30603060
GET_ARG;
3061-
if (arg >= groups)
3061+
if (arg >= (size_t)groups)
30623062
FAIL;
30633063
break;
30643064

@@ -3067,7 +3067,7 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
30673067
'group' is either an integer group number or a group name,
30683068
'then' and 'else' are sub-regexes, and 'else' is optional. */
30693069
GET_ARG;
3070-
if (arg >= groups)
3070+
if (arg >= (size_t)groups)
30713071
FAIL;
30723072
GET_SKIP_ADJ(1);
30733073
code--; /* The skip is relative to the first arg! */
@@ -3096,7 +3096,7 @@ _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
30963096
to allow arbitrary jumps anywhere in the code; so we just look
30973097
for a JUMP opcode preceding our skip target.
30983098
*/
3099-
if (skip >= 3 && skip-3 < end-code &&
3099+
if (skip >= 3 && skip-3 < (Py_uintptr_t)(end - code) &&
31003100
code[skip-3] == SRE_OP_JUMP)
31013101
{
31023102
VTRACE(("both then and else parts present\n"));

0 commit comments

Comments
 (0)