Fix buffer overflows in eve.c (sf.net #3034056)
[monky] / src / temphelper.c
index 48b70f3..c577a1c 100644 (file)
@@ -1,4 +1,7 @@
-/* temphelper.c:  aid in converting temperature units
+/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
+ * vim: ts=4 sw=4 noet ai cindent syntax=c
+ *
+ * temphelper.c:  aid in converting temperature units
  *
  * Copyright (C) 2008 Phil Sutter <Phil@nwl.cc>
  *
 #include <ctype.h>
 #include <sys/types.h>
 #include "temphelper.h"
+#include "conky.h"
 
 /* default to output in celsius */
 static enum TEMP_UNIT output_unit = TEMP_CELSIUS;
 
-static double
-fahrenheit_to_celsius(double n)
+static double fahrenheit_to_celsius(double n)
 {
        return ((n - 32) * 5 / 9);
 }
 
-static double
-celsius_to_fahrenheit(double n)
+static double celsius_to_fahrenheit(double n)
 {
        return ((n * 9 / 5) + 32);
 }
 
-int
-set_temp_output_unit(const char *name)
+int set_temp_output_unit(const char *name)
 {
-       size_t i;
+       long i;
        int rc = 0;
        char *buf;
 
@@ -52,7 +53,7 @@ set_temp_output_unit(const char *name)
                return 1;
 
        buf = strdup(name);
-       for (i = 0; i < strlen(name); i++)
+       for (i = 0; i < (long)strlen(name); i++)
                buf[i] = tolower(name[i]);
 
        if (!strcmp(buf, "celsius"))
@@ -65,8 +66,7 @@ set_temp_output_unit(const char *name)
        return rc;
 }
 
-static double
-convert_temp_output(double n, enum TEMP_UNIT input_unit)
+static double convert_temp_output(double n, enum TEMP_UNIT input_unit)
 {
        if (input_unit == output_unit)
                return n;
@@ -83,14 +83,11 @@ convert_temp_output(double n, enum TEMP_UNIT input_unit)
 
 int temp_print(char *p, size_t p_max_size, double n, enum TEMP_UNIT input_unit)
 {
-       double out, plen;
+       int out;
+       size_t plen;
 
-       out = convert_temp_output(n, input_unit);
+       out = round_to_int_temp(convert_temp_output(n, input_unit));
+       plen = spaced_print(p, p_max_size, "%d", 3, out);
 
-       /* Skip decimal for big values but keep padding sane
-        * (i.e. use 4 chars for them)
-        */
-       plen = snprintf(p, p_max_size, ((out > 100.0) ?
-                                       "%4.0lf" : "%2.1lf") , out);
        return !(plen >= p_max_size);
 }