diff mbox

[i-g-t,01/18] stats: Zero the whole structure at init() time

Message ID 1435417696-28115-2-git-send-email-damien.lespiau@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Lespiau, Damien June 27, 2015, 3:07 p.m. UTC
Because the structure started small, I initialized every member
directly, but that means that the new fields added weren't properly
initialized (sigh!). Zero the whole thing first then.

Also, the punishment for introducing a bug should be to write the
corresponding unit test. It's not a perfect one, but I'll take it.

Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
---
 lib/igt_stats.c       |  4 +++-
 lib/tests/igt_stats.c | 11 +++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)
diff mbox

Patch

diff --git a/lib/igt_stats.c b/lib/igt_stats.c
index 65c0ac6..67ee947 100644
--- a/lib/igt_stats.c
+++ b/lib/igt_stats.c
@@ -23,16 +23,18 @@ 
  */
 
 #include <math.h>
+#include <string.h>
 
 #include "igt_core.h"
 #include "igt_stats.h"
 
 void igt_stats_init(igt_stats_t *stats, unsigned int capacity)
 {
+	memset(stats, 0, sizeof(*stats));
+
 	stats->values = calloc(capacity, sizeof(*stats->values));
 	igt_assert(stats->values);
 	stats->capacity = capacity;
-	stats->n_values = 0;
 }
 
 void igt_stats_fini(igt_stats_t *stats)
diff --git a/lib/tests/igt_stats.c b/lib/tests/igt_stats.c
index f9081c7..f76d334 100644
--- a/lib/tests/igt_stats.c
+++ b/lib/tests/igt_stats.c
@@ -25,6 +25,16 @@ 
 #include "igt_core.h"
 #include "igt_stats.h"
 
+/* Make sure we zero igt_stats_t fields at init() time */
+static void test_init_zero(void)
+{
+	igt_stats_t stats;
+
+	stats.mean = 1.;
+	igt_stats_init(&stats, 2);
+	igt_assert(stats.mean == 0.);
+}
+
 static void test_mean(void)
 {
 	igt_stats_t stats;
@@ -103,6 +113,7 @@  static void test_std_deviation(void)
 
 igt_simple_main
 {
+	test_init_zero();
 	test_mean();
 	test_invalidate_mean();
 	test_std_deviation();