Module Name:    src
Committed By:   kamil
Date:           Wed Jun 13 17:32:29 UTC 2018

Modified Files:
        src/external/gpl2/grep/dist/lib: obstack.c

Log Message:
Avoid Undefind Behavior in DEFAULT_ALIGNMENT in GNU grep(1)

Replace homegrown logic of calculating alignment with alignof, a C11 header
feature.

Add a check for NetBSD >= 8.0 just in case that someone will want to build
it on older NetBSD.

Issue detected with UBSan.


To generate a diff of this commit:
cvs rdiff -u -r1.1.1.1 -r1.2 src/external/gpl2/grep/dist/lib/obstack.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/external/gpl2/grep/dist/lib/obstack.c
diff -u src/external/gpl2/grep/dist/lib/obstack.c:1.1.1.1 src/external/gpl2/grep/dist/lib/obstack.c:1.2
--- src/external/gpl2/grep/dist/lib/obstack.c:1.1.1.1	Sun Jan 10 21:36:18 2016
+++ src/external/gpl2/grep/dist/lib/obstack.c	Wed Jun 13 17:32:29 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: obstack.c,v 1.1.1.1 2016/01/10 21:36:18 christos Exp $	*/
+/*	$NetBSD: obstack.c,v 1.2 2018/06/13 17:32:29 kamil Exp $	*/
 
 /* obstack.c - subroutines used implicitly by object stack macros
    Copyright (C) 1988-1994,96,97,98,99 Free Software Foundation, Inc.
@@ -61,8 +61,23 @@
 
 /* Determine default alignment.  */
 struct fooalign {char x; double d;};
+
+#if defined(__NetBSD__)
+#include <sys/param.h>
+#endif
+
+#ifndef __NetBSD_Prereq__
+#define __NetBSD_Prereq__(a,b,c) 0
+#endif
+
+#if __NetBSD_Prereq__(8,0,0)
+#include <stdalign.h>
+// Avoid Undefined Behavior
+#define DEFAULT_ALIGNMENT ((int)alignof(struct fooalign))
+#else
 #define DEFAULT_ALIGNMENT  \
   ((PTR_INT_TYPE) ((char *) &((struct fooalign *) 0)->d - (char *) 0))
+#endif
 /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
    But in fact it might be less smart and round addresses to as much as
    DEFAULT_ROUNDING.  So we prepare for it to do that.  */

Reply via email to