[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Nano-devel] [PATCH] browser: fix handling of large files
From: |
Mike Frysinger |
Subject: |
[Nano-devel] [PATCH] browser: fix handling of large files |
Date: |
Wed, 15 Jul 2015 06:31:36 -0400 |
The current code relies on the undefined behavior of 1<<40. This fails
because the compiler is free to optimize away checks like (1<<40 != 0).
It also leads to build time warnings because "1" is an integer which is
usually 32bits. The code also assumes that sizeof(long) >= sizeof(off_t)
even though we specifically build in LFS mode, so off_t should be 64bits
while long will still be 32bits on most 32bit systems.
Rather than unwind that though, replace the code with a proper off_t
and by re-using earlier variables so our shift checks always fit into
32bits.
---
src/browser.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/src/browser.c b/src/browser.c
index 3191e55..d5220a7 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -24,6 +24,7 @@
#include "proto.h"
+#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -637,7 +638,7 @@ void browser_refresh(void)
} else
foo = mallocstrcpy(NULL, _("(dir)"));
} else {
- unsigned long result = st.st_size;
+ off_t result = st.st_size;
char modifier;
foo = charalloc(foomaxlen + 1);
@@ -655,10 +656,10 @@ void browser_refresh(void)
modifier = 'G'; /* gigabytes */
}
- /* If less than a terabyte, or if numbers can't even go
- * that high, show the size, otherwise show "(huge)". */
- if (st.st_size < (1 << 40) || (1 << 40) == 0)
- sprintf(foo, "%4lu %cB", result, modifier);
+ /* Show the size if less than a terabyte,
+ * otherwise show "(huge)". */
+ if (modifier != 'G' || result < (1 << 10))
+ sprintf(foo, "%4ju %cB", (intmax_t)result, modifier);
else
/* TRANSLATORS: Try to keep this at most 7 characters.
* If necessary, you can leave out the parentheses. */
--
2.4.4
- [Nano-devel] [PATCH] browser: fix handling of large files,
Mike Frysinger <=