]> git.ipfire.org Git - fireperf.git/commitdiff
util: Statically allocate size buffer
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 24 Sep 2024 14:42:30 +0000 (14:42 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 24 Sep 2024 14:42:30 +0000 (14:42 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/tui.c
src/util.c
src/util.h

index 96863f754d8b3c51afa4acb25cfa100e643bf032..ef23556001d4ebd2e3929166ba3189cce8784a55 100644 (file)
--- a/src/tui.c
+++ b/src/tui.c
@@ -114,6 +114,7 @@ static int fireperf_tui_setup_frame(struct fireperf_tui* tui) {
 
 static int fireperf_tui_update_status(struct fireperf_tui* tui) {
        const struct fireperf_stats* stats = NULL;
+       char buffer[32];
        int r;
 
        int max_x = 0;
@@ -142,11 +143,16 @@ static int fireperf_tui_update_status(struct fireperf_tui* tui) {
        if (r < 0)
                goto ERROR;
 
+       // Fetch peak bandwidth
        size_t peak_bps = fireperf_tui_get_peak_bps(tui, max_x);
 
+       // Format peak bandwidth
+       r = format_size(buffer, peak_bps, FIREPERF_FORMAT_BITS);
+       if (r < 0)
+               goto ERROR;
+
        // Print Peak Bandwidth
-       r = asprintf(&status_l, "%s, Peak Bandwidth: %s", status_l,
-               format_size(peak_bps, FIREPERF_FORMAT_BITS));
+       r = asprintf(&status_l, "%s, Peak Bandwidth: %s", status_l, buffer);
        if (r < 0)
                goto ERROR;
 
@@ -340,7 +346,8 @@ int fireperf_tui_action(struct fireperf_tui* tui) {
 */
 static int fireperf_tui_draw_graph(struct fireperf_tui* tui) {
        const struct fireperf_stats* stats = NULL;
-       char* scale = NULL;
+       char scale[32];
+       int r;
 
        int max_x = 0;
        int max_y = 0;
@@ -361,8 +368,8 @@ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) {
 
        // Draw the grid
        for (double line = 0; line <= 1; line += 0.25) {
-               scale = format_size(line * peak_bps, FIREPERF_FORMAT_BITS);
-               if (!scale)
+               r = format_size(scale, line * peak_bps, FIREPERF_FORMAT_BITS);
+               if (r < 0)
                        continue;
 
                size_t l = strlen(scale);
@@ -374,7 +381,6 @@ static int fireperf_tui_draw_graph(struct fireperf_tui* tui) {
 
                // Write the scale
                mvwaddnstr(tui->graph, max_y - (max_y * line), max_x - l, scale, l);
-               free(scale);
        }
 
        wattroff(tui->graph, COLOR_PAIR(5));
index 558a478eb9c0eae27cc13160fe38779a1de5f7e0..de4715e1ee0119a194301e2c1b180ae58a626fb3 100644 (file)
@@ -18,6 +18,7 @@
 #                                                                             #
 #############################################################################*/
 
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
 static const char* suffixes_bytes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL };
 static const char* suffixes_bits[]  = { "b", "Kb", "Mb", "Gb", "Tb", NULL };
 
-char* format_size(ssize_t size, int unit) {
-       const char** suffixes;
+int __format_size(char* buffer, const size_t length, ssize_t size, const int unit) {
+       const char** suffixes = NULL;
        unsigned int divisor;
+       int r;
 
        switch (unit) {
                case FIREPERF_FORMAT_BYTES:
@@ -45,11 +47,10 @@ char* format_size(ssize_t size, int unit) {
 
                // Invalid input
                default:
-                       return NULL;
+                       return -EINVAL;
        }
 
-       const char** suffix;
-       char* retval = NULL;
+       const char** suffix = NULL;
 
        // Convert into double
        double s = size;
@@ -62,19 +63,19 @@ char* format_size(ssize_t size, int unit) {
        }
 
        if (!*suffix)
-               return NULL;
+               return -EINVAL;
 
        // Format the output string
-       int r = asprintf(&retval, "%.02f %s", s, *suffix);
+       r = snprintf(buffer, length, "%.02f %s", s, *suffix);
        if (r < 0)
-               return NULL;
+               return r;
 
-       return retval;
+       return 0;
 }
 
-static char __timespec[20];
-
 const char* format_timespec(const struct timespec* t) {
+       static char __thread __timespec[20];
+
        // Convert to local time
        struct tm* tm = localtime(&t->tv_sec);
 
index 8315b945d407d0801f2a3235050d994a32792eed..32c3f2ad08c4af261054a36ea8cd231b929c2c38 100644 (file)
@@ -26,6 +26,10 @@ enum {
        FIREPERF_FORMAT_BITS,
 };
 
-char* format_size(ssize_t size, int unit);
+#define format_size(buffer, size, unit) \
+       __format_size(buffer, sizeof(buffer), size, unit)
+
+int __format_size(char* buffer, const size_t length, ssize_t size, const int unit);
+
 const char* format_timespec(const struct timespec* t);
 double timespec_delta(struct timespec* t1, struct timespec* t2);