sfeed: remove undefined behaviour of right shifting a negative value - sfeed - RSS and Atom parser
(HTM) git clone git://git.codemadness.org/sfeed
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 6a6eb12cb0049eb6198b6dfc78aa5f54a742cf57
(DIR) parent b9305cb278e35dc274264824143aa629dda9fe95
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Mon, 1 Dec 2025 21:34:21 +0100
sfeed: remove undefined behaviour of right shifting a negative value
Found by cppcheck.
This rewrites the bitshift hack in a more clear and defined behaviour.
- improve comment for datetounix(): it does no validation of input ranges here
and expects (pre-validated) valid input as if from struct tm fields.
- micro-optimization, -68 is not needed. This is the same for "& 3" also.
Diffstat:
M sfeed.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
---
(DIR) diff --git a/sfeed.c b/sfeed.c
@@ -432,7 +432,7 @@ string_print_timestamp(String *s)
}
/* Convert time fields. Returns a signed (at least) 64-bit UNIX timestamp.
- * Parameters should be passed as they are in a struct tm:
+ * Parameters should be passed as they are in a struct tm and in a valid range:
* that is: year = year - 1900, month = month - 1. */
static long long
datetounix(long long year, int mon, int day, int hour, int min, int sec)
@@ -448,8 +448,8 @@ datetounix(long long year, int mon, int day, int hour, int min, int sec)
/* optimization: handle common range year 1902 up to and including 2038 */
if (year - 2ULL <= 136) {
/* amount of leap days relative to 1970: every 4 years */
- leaps = (year - 68) >> 2;
- if (!((year - 68) & 3)) {
+ leaps = (year / 4) - 17; /* 17 leap years offset for 1902 - 1970 */
+ if (!(year & 3)) {
leaps--;
is_leap = 1;
} else {