vppinfra: fix clib_random_buffer_get_data caching 88/33188/3
authorBenoît Ganne <bganne@cisco.com>
Tue, 20 Jul 2021 14:51:39 +0000 (16:51 +0200)
committerDamjan Marion <dmarion@me.com>
Fri, 20 Aug 2021 11:24:52 +0000 (11:24 +0000)
When using cached bytes:
 - do not overflow
 - do not return the same bytes twice

Type: fix

Change-Id: I2a87b47a79300e56a2201b8fc3cb6cb15b592e28
Signed-off-by: Benoît Ganne <bganne@cisco.com>
src/vppinfra/random_buffer.h

index 320394d..dded531 100644 (file)
@@ -88,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);
@@ -106,7 +106,7 @@ 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;