[sparse] Fix handling of boolean sizes

Jeff Layton jlayton at fedoraproject.org
Thu Jul 17 16:39:08 UTC 2014


commit 3cfe34396c97cba8724e9750ed3df83635c5d524
Author: Jeff Layton <jlayton at primarydata.com>
Date:   Thu Jul 17 12:34:59 2014 -0400

    Fix handling of boolean sizes
    
    sparse handles arrays of bools incorrectly (and likely also structures
    that contain them). Fix this by ensuring the bits_to_bytes rounds the
    size up to the nearest byte instead of down.
    
    Signed-off-by: Jeff Layton <jlayton at primarydata.com>

 ...ke-bits_to_bytes-round-up-instead-of-down.patch |   83 ++++++++++++++++++++
 sparse.spec                                        |   11 ++-
 2 files changed, 91 insertions(+), 3 deletions(-)
---
diff --git a/0001-sparse-make-bits_to_bytes-round-up-instead-of-down.patch b/0001-sparse-make-bits_to_bytes-round-up-instead-of-down.patch
new file mode 100644
index 0000000..6ddf651
--- /dev/null
+++ b/0001-sparse-make-bits_to_bytes-round-up-instead-of-down.patch
@@ -0,0 +1,83 @@
+From 427d3cbf966472a48a297e08e8e75d2da6835867 Mon Sep 17 00:00:00 2001
+From: Jeff Layton <jlayton at primarydata.com>
+Date: Tue, 15 Jul 2014 21:55:15 -0400
+Subject: [PATCH] sparse: make bits_to_bytes round up instead of down
+
+Currently, sparse handles arrays of bools incorrectly. If you declare an
+array of bools like this:
+
+    static _Bool boolarray[3] = {
+        [0] = 1,
+        [1] = 1,
+    };
+
+...you get warnings like this (which are bogus):
+
+    ./test.c:2:10: warning: Initializer entry defined twice
+    ./test.c:3:10:   also defined here
+
+The problem is that bits_to_bytes rounds down instead of up, and sparse
+defaults to _Bool being only 1 bit in size. This causes sparse to think
+that they sit within the same byte, when they do not.
+
+Fix bits_to_bytes to round up instead of down, and fix the call in
+init_ctype to no longer correct for it. Also add a validation
+test to ensure that we don't break this in the future.
+
+Signed-off-by: Jeff Layton <jlayton at primarydata.com>
+---
+ symbol.c                                     |  2 +-
+ target.h                                     |  2 +-
+ validation/initializer-entry-defined-twice.c | 10 ++++++++++
+ 3 files changed, 12 insertions(+), 2 deletions(-)
+
+diff --git a/symbol.c b/symbol.c
+index eb6e1215ee87..f02e07bd8c59 100644
+--- a/symbol.c
++++ b/symbol.c
+@@ -888,7 +888,7 @@ void init_ctype(void)
+ 		struct symbol *sym = ctype->ptr;
+ 		unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
+ 		unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
+-		unsigned long alignment = bits_to_bytes(bit_size + bits_in_char - 1);
++		unsigned long alignment = bits_to_bytes(bit_size);
+ 
+ 		if (alignment > maxalign)
+ 			alignment = maxalign;
+diff --git a/target.h b/target.h
+index 1030c7c38993..140df3c1a64d 100644
+--- a/target.h
++++ b/target.h
+@@ -49,7 +49,7 @@ extern int enum_alignment;
+ 
+ static inline int bits_to_bytes(int bits)
+ {
+-	return bits >= 0 ? bits / bits_in_char : -1;
++	return bits >= 0 ? (bits + bits_in_char - 1) / bits_in_char : -1;
+ }
+ 
+ static inline int bytes_to_bits(int bytes)
+diff --git a/validation/initializer-entry-defined-twice.c b/validation/initializer-entry-defined-twice.c
+index 968e3dd1af2a..8a5bd3a95631 100644
+--- a/validation/initializer-entry-defined-twice.c
++++ b/validation/initializer-entry-defined-twice.c
+@@ -41,6 +41,16 @@ static struct same_offset not_an_error = {
+ 	.field1 = { },
+ 	.field2 = 0
+ };
++
++/*
++ * _Bools generally take a whole byte, so ensure that we can initialize
++ * them without spewing a warning.
++ */
++static _Bool boolarray[3] = {
++	[0] = 1,
++	[1] = 1,
++};
++
+ /*
+  * check-name: Initializer entry defined twice
+  *
+-- 
+1.9.3
+
diff --git a/sparse.spec b/sparse.spec
index 0a009ab..5260c0a 100644
--- a/sparse.spec
+++ b/sparse.spec
@@ -1,15 +1,16 @@
 Name: sparse
 Version: 0.5.0
-Release: 3%{?dist}
+Release: 4%{?dist}
 Summary:    A semantic parser of source files
 Group:      Development/Tools
 License:    MIT
 URL:        https://sparse.wiki.kernel.org
-Source0:    http://www.kernel.org/pub/software/devel/sparse/dist/sparse-%{version}.tar.xz
 BuildRoot:  %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
-
 BuildRequires: libxml2-devel gtk2-devel
 
+Source0:    http://www.kernel.org/pub/software/devel/sparse/dist/sparse-%{version}.tar.xz
+Patch1:     0001-sparse-make-bits_to_bytes-round-up-instead-of-down.patch
+
 %description
 Sparse is a semantic parser of source files: it's neither a compiler
 (although it could be used as a front-end for one) nor is it a
@@ -37,6 +38,7 @@ Development headers headers and static lib for sparse-enabled apps
 
 %prep
 %setup -q
+%patch1 -p1
 
 %define make_destdir \
 make DESTDIR="%{buildroot}" PREFIX="%{_prefix}" \\\
@@ -77,6 +79,9 @@ make clean
 %{_libdir}/pkgconfig/%{name}.pc
 
 %changelog
+* Thu Jul 17 2014 Jeff Layton <jlayton at primarydata.com> - 0.5.0-4
+- Fix handling of boolean sizes
+
 * Sun Jun 15 2014 Jeff Layton <jlayton at primarydata.com> - 0.5.0-3
 - Remove -fpic and -fPIC from CFLAGS. Seems to be causing weird effects with
   -O2. (bz# 1109560)


More information about the scm-commits mailing list