Skip to content

[3.9] Fix compiler warnings in _zoneinfo.c (GH-20342) #20467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 26 additions & 30 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ typedef struct {
PyObject *key;
PyObject *file_repr;
PyObject *weakreflist;
unsigned int num_transitions;
unsigned int num_ttinfos;
size_t num_transitions;
size_t num_ttinfos;
int64_t *trans_list_utc;
int64_t *trans_list_wall[2];
_ttinfo **trans_ttinfos; // References to the ttinfo for each transition
Expand Down Expand Up @@ -117,14 +117,14 @@ ts_to_local(size_t *trans_idx, int64_t *trans_utc, long *utcoff,
static int
parse_tz_str(PyObject *tz_str_obj, _tzrule *out);

static ssize_t
static Py_ssize_t
parse_abbr(const char *const p, PyObject **abbr);
static ssize_t
static Py_ssize_t
parse_tz_delta(const char *const p, long *total_seconds);
static ssize_t
static Py_ssize_t
parse_transition_time(const char *const p, int8_t *hour, int8_t *minute,
int8_t *second);
static ssize_t
static Py_ssize_t
parse_transition_rule(const char *const p, TransitionRuleType **out);

static _ttinfo *
Expand Down Expand Up @@ -891,12 +891,12 @@ load_data(PyZoneInfo_ZoneInfo *self, PyObject *file_obj)

// Load the relevant sizes
Py_ssize_t num_transitions = PyTuple_Size(trans_utc);
if (num_transitions == -1) {
if (num_transitions < 0) {
goto error;
}

Py_ssize_t num_ttinfos = PyTuple_Size(utcoff_list);
if (num_ttinfos == -1) {
if (num_ttinfos < 0) {
goto error;
}

Expand All @@ -908,7 +908,7 @@ load_data(PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
PyMem_Malloc(self->num_transitions * sizeof(int64_t));
trans_idx = PyMem_Malloc(self->num_transitions * sizeof(Py_ssize_t));

for (Py_ssize_t i = 0; i < self->num_transitions; ++i) {
for (size_t i = 0; i < self->num_transitions; ++i) {
PyObject *num = PyTuple_GetItem(trans_utc, i);
if (num == NULL) {
goto error;
Expand Down Expand Up @@ -946,7 +946,7 @@ load_data(PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
if (utcoff == NULL || isdst == NULL) {
goto error;
}
for (Py_ssize_t i = 0; i < self->num_ttinfos; ++i) {
for (size_t i = 0; i < self->num_ttinfos; ++i) {
PyObject *num = PyTuple_GetItem(utcoff_list, i);
if (num == NULL) {
goto error;
Expand Down Expand Up @@ -1468,7 +1468,7 @@ parse_tz_str(PyObject *tz_str_obj, _tzrule *out)
char *p = tz_str;

// Read the `std` abbreviation, which must be at least 3 characters long.
ssize_t num_chars = parse_abbr(p, &std_abbr);
Py_ssize_t num_chars = parse_abbr(p, &std_abbr);
if (num_chars < 1) {
PyErr_Format(PyExc_ValueError, "Invalid STD format in %R", tz_str_obj);
goto error;
Expand Down Expand Up @@ -1565,18 +1565,19 @@ parse_tz_str(PyObject *tz_str_obj, _tzrule *out)
return -1;
}

static ssize_t
parse_uint(const char *const p)
static int
parse_uint(const char *const p, uint8_t *value)
{
if (!isdigit(*p)) {
return -1;
}

return (*p) - '0';
*value = (*p) - '0';
return 0;
}

/* Parse the STD and DST abbreviations from a TZ string. */
static ssize_t
static Py_ssize_t
parse_abbr(const char *const p, PyObject **abbr)
{
const char *ptr = p;
Expand Down Expand Up @@ -1629,7 +1630,7 @@ parse_abbr(const char *const p, PyObject **abbr)
}

/* Parse a UTC offset from a TZ str. */
static ssize_t
static Py_ssize_t
parse_tz_delta(const char *const p, long *total_seconds)
{
// From the POSIX spec:
Expand Down Expand Up @@ -1712,7 +1713,7 @@ parse_tz_delta(const char *const p, long *total_seconds)
}

/* Parse the date portion of a transition rule. */
static ssize_t
static Py_ssize_t
parse_transition_rule(const char *const p, TransitionRuleType **out)
{
// The full transition rule indicates when to change back and forth between
Expand All @@ -1739,20 +1740,18 @@ parse_transition_rule(const char *const p, TransitionRuleType **out)
if (*ptr == 'M') {
uint8_t month, week, day;
ptr++;
ssize_t tmp = parse_uint(ptr);
if (tmp < 0) {
if (parse_uint(ptr, &month)) {
return -1;
}
month = (uint8_t)tmp;
ptr++;
if (*ptr != '.') {
tmp = parse_uint(ptr);
if (tmp < 0) {
uint8_t tmp;
if (parse_uint(ptr, &tmp)) {
return -1;
}

month *= 10;
month += (uint8_t)tmp;
month += tmp;
ptr++;
}

Expand All @@ -1763,18 +1762,15 @@ parse_transition_rule(const char *const p, TransitionRuleType **out)
}
ptr++;

tmp = parse_uint(ptr);
if (tmp < 0) {
if (parse_uint(ptr, values[i])) {
return -1;
}
ptr++;

*(values[i]) = tmp;
}

if (*ptr == '/') {
ptr++;
ssize_t num_chars =
Py_ssize_t num_chars =
parse_transition_time(ptr, &hour, &minute, &second);
if (num_chars < 0) {
return -1;
Expand Down Expand Up @@ -1816,7 +1812,7 @@ parse_transition_rule(const char *const p, TransitionRuleType **out)

if (*ptr == '/') {
ptr++;
ssize_t num_chars =
Py_ssize_t num_chars =
parse_transition_time(ptr, &hour, &minute, &second);
if (num_chars < 0) {
return -1;
Expand All @@ -1840,7 +1836,7 @@ parse_transition_rule(const char *const p, TransitionRuleType **out)
}

/* Parse the time portion of a transition rule (e.g. following an /) */
static ssize_t
static Py_ssize_t
parse_transition_time(const char *const p, int8_t *hour, int8_t *minute,
int8_t *second)
{
Expand Down