[conspire] Cautionary tale: bad NTP client, bad NTP clock, bad date handling, C ints

Ivan Sergio Borgonovo mail at webthatworks.it
Wed Mar 27 03:08:29 PDT 2024



On 3/27/24 2:01 AM, Rick Moen wrote:

> The 2038 problem happens because when the first bit of a 32-bit value is
> 1 and you use it as a signed integer, it’s interpreted as a negative
> number in 2’s complement representation. But C has no protection from
> treating the same value as signed in some contexts and unsigned in
> others. If you start with a signed 32-bit integer with the value -1, it
> is represented in memory as 0xFFFFFFFF. If you then use it as an
> unsigned integer, it becomes the value 4,294,967,296.

I'm once again asking are we here to bash C for no reason?

int main(void) {
   int i = 3;
   unsigned int j = 2;
   int r;
   r = i + j;
}

gcc -Wsign-conversion i.c
i.c: In function ‘main’:
i.c:5:39: warning: conversion to ‘unsigned int’ from ‘int’ may change 
the sign of the result [-Wsign-conversion]
     5 |                                 r = i + j;
       |                                       ^
i.c:5:37: warning: conversion to ‘int’ from ‘unsigned int’ may change 
the sign of the result [-Wsign-conversion]
     5 |                                 r = i + j;
       |

-- 
Ivan Sergio Borgonovo
https://www.webthatworks.it https://www.borgonovo.net





More information about the conspire mailing list