changeset 884:6552b75f3cc3

common: replace blksize static assert with a runtime one Different systems use different sizes for the st_blksize struct stat member and therefore the static assertion will fail on some of them. One such OS is Linux. There, st_blksize is defined as anything between an int and an unsigned long and therefore it is very sensitive to which exact architecture we're on. With all that said, we should return a negated errno instead of asserting. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Sun, 18 Dec 2022 11:40:46 -0500
parents c61456afbba3
children a739778d0f6a
files src/common/attr.c
diffstat 1 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/common/attr.c	Sun Dec 18 10:35:16 2022 -0500
+++ b/src/common/attr.c	Sun Dec 18 11:40:46 2022 -0500
@@ -1,6 +1,6 @@
 /*
  * Copyright (c) 2015 Joshua Kahn <josh@joshuak.net>
- * Copyright (c) 2016-2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2016-2020,2022 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -113,7 +113,16 @@
 	stat->st_blksize = nattr->blksize;
 	stat->st_blocks = nattr->blocks;
 
-	STATIC_ASSERT(sizeof(stat->st_blksize) == sizeof(nattr->blksize));
+	/*
+	 * Sanity check that the blksize value didn't get truncated.  The
+	 * sizeof comparing condition completely elides the check if the
+	 * members' sizes are the same.
+	 *
+	 * FIXME: return a negated erron instead of asserting
+	 */
+	if (sizeof(stat->st_blksize) != sizeof(nattr->blksize))
+		VERIFY3U(stat->st_blksize, ==, nattr->blksize);
+
 	STATIC_ASSERT(sizeof(stat->st_blocks) == sizeof(nattr->blocks));
 
 	/*
@@ -147,7 +156,16 @@
 	nattr->blksize = stat->st_blksize;
 	nattr->blocks = stat->st_blocks;
 
-	STATIC_ASSERT(sizeof(stat->st_blksize) == sizeof(nattr->blksize));
+	/*
+	 * Sanity check that the blksize value didn't get truncated.  The
+	 * sizeof comparing condition completely elides the check if the
+	 * members' sizes are the same.
+	 *
+	 * FIXME: return a negated erron instead of asserting
+	 */
+	if (sizeof(stat->st_blksize) != sizeof(nattr->blksize))
+		VERIFY3U(stat->st_blksize, ==, nattr->blksize);
+
 	STATIC_ASSERT(sizeof(stat->st_blocks) == sizeof(nattr->blocks));
 }