summaryrefslogtreecommitdiffstats
path: root/fdisk/fdisk.c
diff options
context:
space:
mode:
authorKarel Zak2009-10-16 21:49:33 +0200
committerKarel Zak2009-10-17 00:18:46 +0200
commitb2f15782a71fd7d89bed7b3e1f799358779ee828 (patch)
tree0aa8a6148dc02d102aee300e19709f815902b90c /fdisk/fdisk.c
parentRevert "build-sys: move fsck/mkfs for bfs/cramfs/minix to /usr" (diff)
downloadkernel-qcow2-util-linux-b2f15782a71fd7d89bed7b3e1f799358779ee828.tar.gz
kernel-qcow2-util-linux-b2f15782a71fd7d89bed7b3e1f799358779ee828.tar.xz
kernel-qcow2-util-linux-b2f15782a71fd7d89bed7b3e1f799358779ee828.zip
fdisk: fix strict-aliasing bugs
gcc 4.4 produces tons of "dereferencing type-punned pointer will break strict-aliasing rules" warnings for fdisk code where is char buffer[BUFSIZ]; ((struct disklabel *) MBRBuffer)->foo There are two ways how fix the problem: 1/ union { char buffer[BUFSIZ], struct disklabel label } MBRBuffer; 2/ use allocated buffer, this way seems less invasive. This patch implements 2/. Old version: $ make -C fdisk | grep -c warning 236 New version: $ make -C fdisk | grep -c warning 0 About aliasing: - http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40665 - http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html - C99 Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'fdisk/fdisk.c')
-rw-r--r--fdisk/fdisk.c25
1 files changed, 24 insertions, 1 deletions
diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c
index aaa01a84a..d0b4c5f89 100644
--- a/fdisk/fdisk.c
+++ b/fdisk/fdisk.c
@@ -174,7 +174,7 @@ static int type_open = O_RDWR;
* Raw disk label. For DOS-type partition tables the MBR,
* with descriptions of the primary partitions.
*/
-unsigned char MBRbuffer[MAX_SECTOR_SIZE];
+unsigned char *MBRbuffer;
/*
* per partition table entry data
@@ -950,6 +950,27 @@ get_geometry(int fd, struct geom *g) {
}
/*
+ * Please, always use allocated buffer if you want to cast the buffer to
+ * any struct -- cast non-allocated buffer to any struct is against
+ * strict-aliasing rules. --kzak 16-Oct-2009
+ */
+static void init_mbr_buffer(void)
+{
+ if (MBRbuffer)
+ return;
+
+ MBRbuffer = calloc(1, MAX_SECTOR_SIZE);
+ if (!MBRbuffer)
+ fatal(out_of_memory);
+}
+
+void zeroize_mbr_buffer(void)
+{
+ if (MBRbuffer)
+ memset(MBRbuffer, 0, MAX_SECTOR_SIZE);
+}
+
+/*
* Read MBR. Returns:
* -1: no 0xaa55 flag present (possibly entire disk BSD)
* 0: found or created label
@@ -2658,6 +2679,8 @@ main(int argc, char **argv) {
" be used with one specified device\n"));
#endif
+ init_mbr_buffer();
+
if (optl) {
nowarn = 1;
type_open = O_RDONLY;