From 33a09bee3ffed2ecb0af3ed6458fb773695c6eb7 Mon Sep 17 00:00:00 2001 From: millert Date: Wed, 13 Apr 2022 16:23:53 +0000 Subject: [PATCH] ftok: avoid left shift of a signed in by 24 places Fix "left shift of 255 by 24 places cannot be represented in type 'int'" error from UBSAN. Adapted from a FreeBSD diff via tb@. OK tb@ deraadt@ --- lib/libc/gen/ftok.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/libc/gen/ftok.c b/lib/libc/gen/ftok.c index ea1edf1afd9..9edcd30005a 100644 --- a/lib/libc/gen/ftok.c +++ b/lib/libc/gen/ftok.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ftok.c,v 1.9 2019/06/28 13:32:41 deraadt Exp $ */ +/* $OpenBSD: ftok.c,v 1.10 2022/04/13 16:23:53 millert Exp $ */ /* * Copyright (c) 1994 SigmaSoft, Th. Lockert * All rights reserved. @@ -32,11 +32,12 @@ key_t ftok(const char *path, int id) { + const unsigned int u_id = id; struct stat st; if (stat(path, &st) == -1) return (key_t)-1; return (key_t) - ((id & 0xff) << 24 | (st.st_dev & 0xff) << 16 | (st.st_ino & 0xffff)); + ((u_id & 0xff) << 24 | (st.st_dev & 0xff) << 16 | (st.st_ino & 0xffff)); } -- 2.20.1