cleanup safety checks
[liboping.git] / src / oping.c
index ddf03e5..a630b8d 100644 (file)
@@ -610,6 +610,9 @@ static int update_stats_from_context (ping_context_t *ctx, pingobj_iter_t *iter)
 {
        double latency = -1.0;
        size_t buffer_len = sizeof (latency);
+        int maxx;
+        getmaxyx(ctx->window, maxx, maxx);
+
        ping_iterator_get_info (iter, PING_INFO_LATENCY,
                        &latency, &buffer_len);
 
@@ -661,6 +664,9 @@ static int update_stats_from_context (ping_context_t *ctx, pingobj_iter_t *iter)
                         int index = 0;
 
                         ratio = latency / PING_DEF_TTL;
+                        if (ratio > 1) {
+                          ratio = 1;
+                        }
                         if (ratio > 2/3.0) {
                           color = OPING_RED_HIST;
                         }
@@ -671,12 +677,16 @@ static int update_stats_from_context (ping_context_t *ctx, pingobj_iter_t *iter)
                         /* HOST_PRINTF ("%%r%f-ia%d-", ratio, index); */
                         index = index % (BARS_LEN-1);
                         /* HOST_PRINTF ("im%d-", index); */
-                        if (index < 0 || index >= BARS_LEN) {
+                        if (index < 0) {
                           index = 0; /* safety check */
                         }
+                        if (index >= BARS_LEN) {
+                          index = BARS_LEN -1; /* safety check */
+                        }
                         wattron (ctx->window, COLOR_PAIR(color));
                         mvwprintw (ctx->window,
-                                   /* y = */ 3, /* x = */ 1 + sequence, 
+                                   /* y = */ 3,
+                                   /* x = */ (1 + sequence) % maxx,
                                    bars[index]);
                        wattroff (ctx->window, COLOR_PAIR(color));
                }
@@ -687,7 +697,8 @@ static int update_stats_from_context (ping_context_t *ctx, pingobj_iter_t *iter)
         else {
                 wattron (ctx->window, COLOR_PAIR(OPING_RED) | A_BOLD);
                 mvwprintw (ctx->window,
-                           /* y = */ 3, /* x = */ 1 + sequence, 
+                           /* y = */ 3,
+                           /* x = */ (1 + sequence) % maxx,
                            "!");
                 wattroff (ctx->window, COLOR_PAIR(OPING_RED) | A_BOLD);
         }