Skip to content

Commit 54d634f

Browse files
committed
struct: fix align()
1 parent a13a47a commit 54d634f

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

Modules/_struct.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,18 +1583,29 @@ whichtable(const char **pfmt)
15831583
}
15841584

15851585

1586+
static int
1587+
format_equal(const formatdef *e, const char *s)
1588+
{
1589+
const char *format = e->format;
1590+
size_t i = 0;
1591+
while (format[i] == s[i]) {
1592+
i++;
1593+
if (format[i] == 0) {
1594+
return 1;
1595+
}
1596+
}
1597+
return 0;
1598+
}
1599+
1600+
15861601
/* Get the table entry for a format code */
15871602

15881603
static const formatdef *
15891604
getentry(_structmodulestate *state, const char *s, const formatdef *f)
15901605
{
15911606
for (; f->format != NULL; f++) {
1592-
size_t i = 0;
1593-
while (f->format[i] == s[i]) {
1594-
i++;
1595-
if (f->format[i] == 0) {
1596-
return f;
1597-
}
1607+
if (format_equal(f, s)) {
1608+
return f;
15981609
}
15991610
}
16001611
PyErr_SetString(state->StructError, "bad char in struct format");
@@ -1609,7 +1620,7 @@ align(Py_ssize_t size, const char *s, const formatdef *e)
16091620
{
16101621
Py_ssize_t extra;
16111622

1612-
if (strcmp(e->format, s) == 0) {
1623+
if (format_equal(e, s)) {
16131624
if (e->alignment && size > 0) {
16141625
extra = (e->alignment - 1) - (size - 1) % (e->alignment);
16151626
if (extra > PY_SSIZE_T_MAX - size)

0 commit comments

Comments
 (0)