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.
This commit is contained in:
kamil 2018-06-13 17:32:29 +00:00
parent 970eaab5e8
commit 8317e0eadb
1 changed files with 16 additions and 1 deletions

View File

@ -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. */