Skip to content

Commit 6343486

Browse files
authored
pythongh-121165: protect macro expansion of ADJUST_INDICES with do-while(0) (python#121166)
1 parent 15232a0 commit 6343486

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

Objects/bytes_methods.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -432,19 +432,24 @@ parse_args_finds_byte(const char *function_name, PyObject **subobj, char *byte)
432432
}
433433

434434
/* helper macro to fixup start/end slice values */
435-
#define ADJUST_INDICES(start, end, len) \
436-
if (end > len) \
437-
end = len; \
438-
else if (end < 0) { \
439-
end += len; \
440-
if (end < 0) \
441-
end = 0; \
442-
} \
443-
if (start < 0) { \
444-
start += len; \
445-
if (start < 0) \
446-
start = 0; \
447-
}
435+
#define ADJUST_INDICES(start, end, len) \
436+
do { \
437+
if (end > len) { \
438+
end = len; \
439+
} \
440+
else if (end < 0) { \
441+
end += len; \
442+
if (end < 0) { \
443+
end = 0; \
444+
} \
445+
} \
446+
if (start < 0) { \
447+
start += len; \
448+
if (start < 0) { \
449+
start = 0; \
450+
} \
451+
} \
452+
} while (0)
448453

449454
Py_LOCAL_INLINE(Py_ssize_t)
450455
find_internal(const char *str, Py_ssize_t len,

Objects/unicodeobject.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9315,19 +9315,24 @@ _PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode)
93159315
/* --- Helpers ------------------------------------------------------------ */
93169316

93179317
/* helper macro to fixup start/end slice values */
9318-
#define ADJUST_INDICES(start, end, len) \
9319-
if (end > len) \
9320-
end = len; \
9321-
else if (end < 0) { \
9322-
end += len; \
9323-
if (end < 0) \
9324-
end = 0; \
9325-
} \
9326-
if (start < 0) { \
9327-
start += len; \
9328-
if (start < 0) \
9329-
start = 0; \
9330-
}
9318+
#define ADJUST_INDICES(start, end, len) \
9319+
do { \
9320+
if (end > len) { \
9321+
end = len; \
9322+
} \
9323+
else if (end < 0) { \
9324+
end += len; \
9325+
if (end < 0) { \
9326+
end = 0; \
9327+
} \
9328+
} \
9329+
if (start < 0) { \
9330+
start += len; \
9331+
if (start < 0) { \
9332+
start = 0; \
9333+
} \
9334+
} \
9335+
} while (0)
93319336

93329337
static Py_ssize_t
93339338
any_find_slice(PyObject* s1, PyObject* s2,

0 commit comments

Comments
 (0)