summaryrefslogtreecommitdiffstats
path: root/fb-upload.c.in
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@server-speed.net>2011-04-19 19:19:06 +0200
committerFlorian Pritz <bluewind@server-speed.net>2011-04-19 19:19:06 +0200
commit412dc1487390f6fde342a68e025453b3f9c6b5c6 (patch)
tree2cb66e497accbb19cb436fb9568c6ffb3614095e /fb-upload.c.in
parent0d75fdb177dd58811162e40d2be80e62b0b62e4c (diff)
fix segfault in format_bytesv0.7.2
0 or very big values can cause unwanted behavior so fall back to the highest suffix for big ones or to bytes for small ones. Signed-off-by: Florian Pritz <bluewind@server-speed.net>
Diffstat (limited to 'fb-upload.c.in')
-rw-r--r--fb-upload.c.in15
1 files changed, 13 insertions, 2 deletions
diff --git a/fb-upload.c.in b/fb-upload.c.in
index ff0a8ab..23fa8e0 100644
--- a/fb-upload.c.in
+++ b/fb-upload.c.in
@@ -22,6 +22,8 @@
#include <curl/types.h>
#include <curl/easy.h>
+#define FORMAT_ARRAY_SIZE 5
+
/* struct which holds the persistent data for progress_callback */
struct progressData {
struct timeval starttime;
@@ -62,11 +64,20 @@ int load_file(const char *fn, char **data, size_t *data_size)
void format_bytes(double bytes, char *buf)
{
double base = 0;
- char suffix[][4] = {"B", "KiB", "MiB", "GiB", "TiB"};
+ int suffix_pos = 0;
+ char suffix[FORMAT_ARRAY_SIZE][4] = {"B", "KiB", "MiB", "GiB", "TiB"};
base = log(bytes) / log(1024);
+ suffix_pos = (int)floor(base);
+
+ if (suffix_pos >= FORMAT_ARRAY_SIZE)
+ suffix_pos = FORMAT_ARRAY_SIZE-1;
- snprintf(buf, 64, "%.2f%s", pow(1024, base - floor(base)), suffix[(int)floor(base)]);
+ if (suffix_pos > 0) {
+ snprintf(buf, 64, "%.2f%s", pow(1024, base - suffix_pos), suffix[suffix_pos]);
+ } else {
+ snprintf(buf, 64, "%.2fB", bytes);
+ }
}
int progress_callback(void *cb_data,