From c91d0f58dc94074cdc77c40c39ecf8e80d7b82e4 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Fri, 7 Aug 2015 15:06:24 +0100 Subject: [PATCH] tools/libxl: Alter the use of rand() in testidl Coverity warns for every occurrence of rand(), which is made worse because each time the IDL changes, some of the calls get re-flagged. Collect all calls to rand() in a single function, test_rand(), which takes a modulo parameter for convenience. This turns 40 defects currently into 1, which won't get re-flagged when the IDL changes. In addition, fix the erroneous random choice for libxl_defbool_set(). "!!rand() % 1" is unconditionally 0, and even without the "% 1" would still be very heavily skewed in one direction. Signed-off-by: Andrew Cooper Acked-by: Wei Liu Acked-by: Ian Campbell --- tools/libxl/gentest.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/tools/libxl/gentest.py b/tools/libxl/gentest.py index 85311e79de..989959fc68 100644 --- a/tools/libxl/gentest.py +++ b/tools/libxl/gentest.py @@ -30,7 +30,7 @@ def gen_rand_init(ty, v, indent = " ", parent = None): elif isinstance(ty, idl.Array): if parent is None: raise Exception("Array type must have a parent") - s += "%s = rand()%%8;\n" % (parent + ty.lenvar.name) + s += "%s = test_rand(8);\n" % (parent + ty.lenvar.name) s += "%s = calloc(%s, sizeof(*%s));\n" % \ (v, parent + ty.lenvar.name, v) s += "assert(%s);\n" % (v, ) @@ -64,13 +64,13 @@ def gen_rand_init(ty, v, indent = " ", parent = None): elif ty.typename in ["libxl_uuid", "libxl_mac", "libxl_hwcap", "libxl_ms_vm_genid"]: s += "rand_bytes((uint8_t *)%s, sizeof(*%s));\n" % (v,v) elif ty.typename in ["libxl_domid", "libxl_devid"] or isinstance(ty, idl.Number): - s += "%s = rand() %% (sizeof(%s)*8);\n" % \ + s += "%s = test_rand(sizeof(%s) * 8);\n" % \ (ty.pass_arg(v, parent is None), ty.pass_arg(v, parent is None)) elif ty.typename in ["bool"]: - s += "%s = rand() %% 2;\n" % v + s += "%s = test_rand(2);\n" % v elif ty.typename in ["libxl_defbool"]: - s += "libxl_defbool_set(%s, !!rand() %% 1);\n" % v + s += "libxl_defbool_set(%s, test_rand(2));\n" % v elif ty.typename in ["char *"]: s += "%s = rand_str();\n" % v elif ty.private: @@ -104,13 +104,19 @@ if __name__ == '__main__': #include "libxl.h" #include "libxl_utils.h" +static int test_rand(unsigned max) +{ + /* We are not using rand() for its cryptographic properies. */ + return rand() % max; +} + static char *rand_str(void) { - int i, sz = rand() % 32; + int i, sz = test_rand(32); char *s = malloc(sz+1); assert(s); for (i=0; isize = rand() % 16; + bitmap->size = test_rand(16); bitmap->map = calloc(bitmap->size, sizeof(*bitmap->map)); assert(bitmap->map); libxl_for_each_bit(i, *bitmap) { - if (rand() % 2) + if (test_rand(2)) libxl_bitmap_set(bitmap, i); else libxl_bitmap_reset(bitmap, i); @@ -138,13 +144,13 @@ static void libxl_bitmap_rand_init(libxl_bitmap *bitmap) static void libxl_key_value_list_rand_init(libxl_key_value_list *pkvl) { - int i, nr_kvp = rand() % 16; + int i, nr_kvp = test_rand(16); libxl_key_value_list kvl = calloc(nr_kvp+1, 2*sizeof(char *)); assert(kvl); for (i = 0; i<2*nr_kvp; i += 2) { kvl[i] = rand_str(); - if (rand() % 8) + if (test_rand(8)) kvl[i+1] = rand_str(); else kvl[i+1] = NULL; @@ -156,7 +162,7 @@ static void libxl_key_value_list_rand_init(libxl_key_value_list *pkvl) static void libxl_cpuid_policy_list_rand_init(libxl_cpuid_policy_list *pp) { - int i, nr_policies = rand() % 16; + int i, nr_policies = test_rand(16); struct { const char *n; int w; @@ -189,8 +195,8 @@ static void libxl_cpuid_policy_list_rand_init(libxl_cpuid_policy_list *pp) libxl_cpuid_policy_list p = NULL; for (i = 0; i < nr_policies; i++) { - int opt = rand() % nr_options; - int val = rand() % (1<