Module Name: src
Committed By: kamil
Date: Thu Jul 26 00:05:28 UTC 2018
Modified Files:
src/lib/libc/gen: ftok.c
Log Message:
Avoid undefined behavior in ftok(3)
Do not change the signedness bit with a left shift operation.
Cast to unsigned integer to prevent this.
ftok.c:56:10, left shift of 123456789 by 24 places cannot be represented in
type 'int'
ftok.c:56:10, left shift of 4160 by 24 places cannot be represented in type
'int'
Detected with micro-UBSan in the user mode.
To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/lib/libc/gen/ftok.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libc/gen/ftok.c
diff -u src/lib/libc/gen/ftok.c:1.11 src/lib/libc/gen/ftok.c:1.12
--- src/lib/libc/gen/ftok.c:1.11 Tue Mar 20 16:36:05 2012
+++ src/lib/libc/gen/ftok.c Thu Jul 26 00:05:28 2018
@@ -1,4 +1,4 @@
-/* $NetBSD: ftok.c,v 1.11 2012/03/20 16:36:05 matt Exp $ */
+/* $NetBSD: ftok.c,v 1.12 2018/07/26 00:05:28 kamil Exp $ */
/*
* Copyright (c) 1994 SigmaSoft, Th. Lockert <[email protected]>
@@ -27,7 +27,7 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: ftok.c,v 1.11 2012/03/20 16:36:05 matt Exp $");
+__RCSID("$NetBSD: ftok.c,v 1.12 2018/07/26 00:05:28 kamil Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
@@ -53,5 +53,6 @@ ftok(const char *path, int id)
return (key_t)-1;
return (key_t)
- (id << 24 | (st.st_dev & 0xff) << 16 | (st.st_ino & 0xffff));
+ ((unsigned int)id << 24 | (st.st_dev & 0xff) << 16 |
+ (st.st_ino & 0xffff));
}