Re: [PATCH] libuuid: Fix pre-1970 UUID v1 timestamp wraparound
Thomas Weißschuh <[email protected]> Sun, 14 Dec 2025 08:36:31 +0900 (GMT+09:00)
| Newsgroups | org.kernel.vger.util-linux |
|---|---|
| Message-ID | <[email protected]> |
Hey Kiran, thanks for your patch! Dec 14, 2025 05:04:23 Kiran Rangoon <[email protected]>: > gregorian_to_unix now returns -1 and sets errno=EOVERFLOW > for timestamps before the Unix epoch. uuid_time_v1 and uuid_time_v6 > now use signed arithmetic to prevent unsigned wraparound. > > This fixes uuidparse displaying far-future dates for historical UUIDs. Can you add an example for the issue here? > > Signed-off-by: Kiran Rangoon <[email protected]> > --- > libuuid/src/uuid_time.c | 15 +++++++++++---- > 1 file changed, 11 insertions(+), 4 deletions(-) Could you also add some tests? > > diff --git a/libuuid/src/uuid_time.c b/libuuid/src/uuid_time.c > index c7516152b..b7fcc892d 100644 > --- a/libuuid/src/uuid_time.c > +++ b/libuuid/src/uuid_time.c > @@ -60,15 +60,22 @@ > /* prototype to make compiler happy */ > time_t __uuid_time(const uuid_t uu, struct timeval *ret_tv); > > -static uint64_t gregorian_to_unix(uint64_t ts) > +static int64_t gregorian_to_unix(uint64_t ts) > { > - return ts - ((((uint64_t) 0x01B21DD2) << 32) + 0x13814000); > + uint64_t offset = ((((uint64_t) 0x01B21DD2) << 32) + 0x13814000); In general, IMO we should just use a 64bit constant here instead of the calculation. (Not your fault obviously) > + > + if (ts < offset) { > + errno = EOVERFLOW; > + return -1; Instead of erroring out here, do you think it would be possible to gracefully handle such negative values through changes in some other places? > + } > + > + return ts - offset; > } > > static void uuid_time_v1(const struct uuid *uuid, struct timeval *tv) > { > uint32_t high; > - uint64_t clock_reg; > + int64_t clock_reg; > > high = uuid->time_mid | ((uuid->time_hi_and_version & 0xFFF) << 16); > clock_reg = uuid->time_low | ((uint64_t) high << 32); > @@ -80,7 +87,7 @@ static void uuid_time_v1(const struct uuid *uuid, struct timeval *tv) > > static void uuid_time_v6(const struct uuid *uuid, struct timeval *tv) > { > - uint64_t clock_reg; > + int64_t clock_reg; > > clock_reg = uuid->time_low; > clock_reg <<= 16; > -- > 2.47.3