Fix compiler warnings in _zoneinfo.c (GH-20342)

```
D:\a\cpython\cpython\Modules\_zoneinfo.c(903,52): warning C4267: '=': conversion from 'size_t' to 'unsigned int', possible loss of data [D:\a\cpython\cpython\PCbuild\_zoneinfo.vcxproj]
D:\a\cpython\cpython\Modules\_zoneinfo.c(904,44): warning C4267: '=': conversion from 'size_t' to 'unsigned int', possible loss of data [D:\a\cpython\cpython\PCbuild\_zoneinfo.vcxproj]
D:\a\cpython\cpython\Modules\_zoneinfo.c(1772,31): warning C4244: '=': conversion from 'ssize_t' to 'uint8_t', possible loss of data [D:\a\cpython\cpython\PCbuild\_zoneinfo.vcxproj]
```
(cherry picked from commit e4799b9594)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
This commit is contained in:
Miss Islington (bot) 2020-05-27 14:08:20 -07:00 committed by GitHub
parent 9b83829e7d
commit be240b8571
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -36,8 +36,8 @@ typedef struct {
PyObject *key; PyObject *key;
PyObject *file_repr; PyObject *file_repr;
PyObject *weakreflist; PyObject *weakreflist;
unsigned int num_transitions; size_t num_transitions;
unsigned int num_ttinfos; size_t num_ttinfos;
int64_t *trans_list_utc; int64_t *trans_list_utc;
int64_t *trans_list_wall[2]; int64_t *trans_list_wall[2];
_ttinfo **trans_ttinfos; // References to the ttinfo for each transition _ttinfo **trans_ttinfos; // References to the ttinfo for each transition
@ -117,14 +117,14 @@ ts_to_local(size_t *trans_idx, int64_t *trans_utc, long *utcoff,
static int static int
parse_tz_str(PyObject *tz_str_obj, _tzrule *out); parse_tz_str(PyObject *tz_str_obj, _tzrule *out);
static ssize_t static Py_ssize_t
parse_abbr(const char *const p, PyObject **abbr); 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); 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, parse_transition_time(const char *const p, int8_t *hour, int8_t *minute,
int8_t *second); int8_t *second);
static ssize_t static Py_ssize_t
parse_transition_rule(const char *const p, TransitionRuleType **out); parse_transition_rule(const char *const p, TransitionRuleType **out);
static _ttinfo * static _ttinfo *
@ -891,12 +891,12 @@ load_data(PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
// Load the relevant sizes // Load the relevant sizes
Py_ssize_t num_transitions = PyTuple_Size(trans_utc); Py_ssize_t num_transitions = PyTuple_Size(trans_utc);
if (num_transitions == -1) { if (num_transitions < 0) {
goto error; goto error;
} }
Py_ssize_t num_ttinfos = PyTuple_Size(utcoff_list); Py_ssize_t num_ttinfos = PyTuple_Size(utcoff_list);
if (num_ttinfos == -1) { if (num_ttinfos < 0) {
goto error; goto error;
} }
@ -908,7 +908,7 @@ load_data(PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
PyMem_Malloc(self->num_transitions * sizeof(int64_t)); PyMem_Malloc(self->num_transitions * sizeof(int64_t));
trans_idx = PyMem_Malloc(self->num_transitions * sizeof(Py_ssize_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); PyObject *num = PyTuple_GetItem(trans_utc, i);
if (num == NULL) { if (num == NULL) {
goto error; goto error;
@ -946,7 +946,7 @@ load_data(PyZoneInfo_ZoneInfo *self, PyObject *file_obj)
if (utcoff == NULL || isdst == NULL) { if (utcoff == NULL || isdst == NULL) {
goto error; 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); PyObject *num = PyTuple_GetItem(utcoff_list, i);
if (num == NULL) { if (num == NULL) {
goto error; goto error;
@ -1468,7 +1468,7 @@ parse_tz_str(PyObject *tz_str_obj, _tzrule *out)
char *p = tz_str; char *p = tz_str;
// Read the `std` abbreviation, which must be at least 3 characters long. // 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) { if (num_chars < 1) {
PyErr_Format(PyExc_ValueError, "Invalid STD format in %R", tz_str_obj); PyErr_Format(PyExc_ValueError, "Invalid STD format in %R", tz_str_obj);
goto error; goto error;
@ -1565,18 +1565,19 @@ error:
return -1; return -1;
} }
static ssize_t static int
parse_uint(const char *const p) parse_uint(const char *const p, uint8_t *value)
{ {
if (!isdigit(*p)) { if (!isdigit(*p)) {
return -1; return -1;
} }
return (*p) - '0'; *value = (*p) - '0';
return 0;
} }
/* Parse the STD and DST abbreviations from a TZ string. */ /* 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) parse_abbr(const char *const p, PyObject **abbr)
{ {
const char *ptr = p; const char *ptr = p;
@ -1629,7 +1630,7 @@ parse_abbr(const char *const p, PyObject **abbr)
} }
/* Parse a UTC offset from a TZ str. */ /* 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) parse_tz_delta(const char *const p, long *total_seconds)
{ {
// From the POSIX spec: // From the POSIX spec:
@ -1712,7 +1713,7 @@ complete:
} }
/* Parse the date portion of a transition rule. */ /* Parse the date portion of a transition rule. */
static ssize_t static Py_ssize_t
parse_transition_rule(const char *const p, TransitionRuleType **out) parse_transition_rule(const char *const p, TransitionRuleType **out)
{ {
// The full transition rule indicates when to change back and forth between // The full transition rule indicates when to change back and forth between
@ -1739,20 +1740,18 @@ parse_transition_rule(const char *const p, TransitionRuleType **out)
if (*ptr == 'M') { if (*ptr == 'M') {
uint8_t month, week, day; uint8_t month, week, day;
ptr++; ptr++;
ssize_t tmp = parse_uint(ptr); if (parse_uint(ptr, &month)) {
if (tmp < 0) {
return -1; return -1;
} }
month = (uint8_t)tmp;
ptr++; ptr++;
if (*ptr != '.') { if (*ptr != '.') {
tmp = parse_uint(ptr); uint8_t tmp;
if (tmp < 0) { if (parse_uint(ptr, &tmp)) {
return -1; return -1;
} }
month *= 10; month *= 10;
month += (uint8_t)tmp; month += tmp;
ptr++; ptr++;
} }
@ -1763,18 +1762,15 @@ parse_transition_rule(const char *const p, TransitionRuleType **out)
} }
ptr++; ptr++;
tmp = parse_uint(ptr); if (parse_uint(ptr, values[i])) {
if (tmp < 0) {
return -1; return -1;
} }
ptr++; ptr++;
*(values[i]) = tmp;
} }
if (*ptr == '/') { if (*ptr == '/') {
ptr++; ptr++;
ssize_t num_chars = Py_ssize_t num_chars =
parse_transition_time(ptr, &hour, &minute, &second); parse_transition_time(ptr, &hour, &minute, &second);
if (num_chars < 0) { if (num_chars < 0) {
return -1; return -1;
@ -1816,7 +1812,7 @@ parse_transition_rule(const char *const p, TransitionRuleType **out)
if (*ptr == '/') { if (*ptr == '/') {
ptr++; ptr++;
ssize_t num_chars = Py_ssize_t num_chars =
parse_transition_time(ptr, &hour, &minute, &second); parse_transition_time(ptr, &hour, &minute, &second);
if (num_chars < 0) { if (num_chars < 0) {
return -1; return -1;
@ -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 /) */ /* 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, parse_transition_time(const char *const p, int8_t *hour, int8_t *minute,
int8_t *second) int8_t *second)
{ {