vppinfra: fix clib_random_buffer_get_data caching
[vpp.git] / src / vppinfra / random_buffer.h
index eb31854..dded531 100644 (file)
 
 #include <vppinfra/clib.h>
 #include <vppinfra/random_isaac.h>
+#include <vppinfra/warnings.h>
+
+/* *INDENT-OFF* */
+WARN_OFF(array-bounds)
+/* *INDENT-ON* */
 
 typedef struct
 {
@@ -83,7 +88,7 @@ clib_random_buffer_get_data (clib_random_buffer_t * b, uword n_bytes)
   if (n_bytes <= l)
     {
       b->n_cached_bytes = l - n_bytes;
-      return &b->cached_bytes[l];
+      return &b->cached_bytes[l - n_bytes];
     }
 
   n_words = n_bytes / sizeof (uword);
@@ -101,12 +106,16 @@ clib_random_buffer_get_data (clib_random_buffer_t * b, uword n_bytes)
     {
       b->cached_word = b->buffer[i];
       b->n_cached_bytes = sizeof (uword) - n_bytes;
-      return b->cached_bytes;
+      return &b->cached_bytes[sizeof (uword) - n_bytes];
     }
   else
     return b->buffer + i;
 }
 
+/* *INDENT-OFF* */
+WARN_ON(array-bounds)
+/* *INDENT-ON* */
+
 #endif /* included_clib_random_buffer_h */
 
 /*