From: Ben Pfaff Date: Fri, 7 Nov 2014 17:16:08 +0000 (-0800) Subject: Check category indexes. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7b655ed0e0c7283765978b4b3975a320f10d8a4b;p=pspp Check category indexes. --- diff --git a/dump.c b/dump.c index 63724092f3..158caace62 100644 --- a/dump.c +++ b/dump.c @@ -307,8 +307,33 @@ dump_value__(int level, bool match1) } } +static int +compare_int(const void *a_, const void *b_) +{ + const int *a = a_; + const int *b = b_; + return *a < *b ? -1 : *a > *b; +} + +static void +check_permutation(int *a, int n, const char *name) +{ + int b[n]; + memcpy(b, a, n * sizeof *a); + qsort(b, n, sizeof *b, compare_int); + for (int i = 0; i < n; i++) + if (b[i] != i) + { + fprintf(stderr, "bad %s permutation:", name); + for (int i = 0; i < n; i++) + fprintf(stderr, " %d", a[i]); + putc('\n', stderr); + exit(1); + } +} + static void -dump_category(int level) +dump_category(int level, int *indexes, int *n_indexes) { dump_value__ (level, true); match_byte(0); @@ -329,16 +354,22 @@ dump_category(int level) int indx = get_u32(); int n_categories = get_u32(); - if (indx != -1 && n_categories != 0) + if (indx != -1) { - fprintf(stderr, "index not -1 but subcategories\n"); - exit(1); + if (n_categories != 0) + { + fprintf(stderr, "index not -1 but subcategories\n"); + exit(1); + } + indexes[(*n_indexes)++] = indx; } if (n_categories > 0) printf (", %d subcategories:", n_categories); + else + printf (", index %d", indx); printf("\n"); for (int i = 0; i < n_categories; i++) - dump_category (level + 1); + dump_category (level + 1, indexes, n_indexes); } static void @@ -362,8 +393,12 @@ dump_dim(void) match_byte(0); n_categories = get_u32(); printf("%d nested categories\n", n_categories); + + int indexes[1024]; + int n_indexes = 0; for (int i = 0; i < n_categories; i++) - dump_category (0); + dump_category (0, indexes, &n_indexes); + check_permutation(indexes, n_indexes, "categories"); } int n_dims; @@ -379,14 +414,6 @@ dump_dims(void) } } -static int -compare_int(const void *a_, const void *b_) -{ - const int *a = a_; - const int *b = b_; - return *a < *b ? -1 : *a > *b; -} - static void dump_data(void) { @@ -396,19 +423,10 @@ dump_data(void) match_u32_assert(n_dims - t); /* The next n_dims numbers are a permutation of the dimension numbers. */ - int a[n_dims], b[n_dims]; - for (int i = 0; i < n_dims; i++) - a[i] = b[i] = get_u32(); - qsort(b, n_dims, sizeof *b, compare_int); + int a[n_dims]; for (int i = 0; i < n_dims; i++) - if (b[i] != i) - { - fprintf(stderr, "bad dimension permutation:"); - for (int i = 0; i < n_dims; i++) - fprintf(stderr, " %d", a[i]); - putc('\n', stderr); - exit(1); - } + a[i] = get_u32(); + check_permutation(a, n_dims, "dimensions"); int x = get_u32(); printf ("%d data values, starting at %08x\n", x, pos);