Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/gpl2/grep/dist/lib Avoid Undefind Behavior in DEFAU...



details:   https://anonhg.NetBSD.org/src/rev/032e7a1ce265
branches:  trunk
changeset: 319837:032e7a1ce265
user:      kamil <kamil%NetBSD.org@localhost>
date:      Wed Jun 13 17:32:29 2018 +0000

description:
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.

diffstat:

 external/gpl2/grep/dist/lib/obstack.c |  17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)

diffs (33 lines):

diff -r a75cb9b9f7b8 -r 032e7a1ce265 external/gpl2/grep/dist/lib/obstack.c
--- a/external/gpl2/grep/dist/lib/obstack.c     Wed Jun 13 16:03:10 2018 +0000
+++ b/external/gpl2/grep/dist/lib/obstack.c     Wed Jun 13 17:32:29 2018 +0000
@@ -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.  */



Home | Main Index | Thread Index | Old Index