From: Ben Pfaff Date: Tue, 12 May 2009 03:08:19 +0000 (-0700) Subject: Make value_set_missing(), etc. tolerate values of width -1. X-Git-Tag: v0.7.3~80 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=90a74c873f5dc594ac9bb6b1b347bf12b1c00319;p=pspp-builds.git Make value_set_missing(), etc. tolerate values of width -1. In some circumstances a value of width -1 crops up, e.g. when a case is made from a dictionary that has had a variable deleted in the middle. Such a value has no content at all. In the long run it should be possible to get rid of these values entirely--their presence is a wart--but for now the case and value code needs to tolerate them. This fixes a segfault in the GUI when inserting a new case when the datasheet case has a column with width -1 (due to deletion of a variable), which was caused by case_set_missing() calling value_set_missing() for the -1 width variable, which in turn was writing through an invalid pointer. --- diff --git a/src/data/value.c b/src/data/value.c index 2341f029..71a2dc94 100644 --- a/src/data/value.c +++ b/src/data/value.c @@ -91,10 +91,13 @@ value_copy_buf_rpad (union value *dst, int dst_width, void value_set_missing (union value *v, int width) { - if (width == 0) - v->f = SYSMIS; - else - memset (value_str_rw (v, width), ' ', width); + if (width != -1) + { + if (width == 0) + v->f = SYSMIS; + else + memset (value_str_rw (v, width), ' ', width); + } } /* Compares A and B, which both have the given WIDTH, and returns @@ -102,8 +105,8 @@ value_set_missing (union value *v, int width) int value_compare_3way (const union value *a, const union value *b, int width) { - return (width == 0 - ? (a->f < b->f ? -1 : a->f > b->f) + return (width == -1 ? 0 + : width == 0 ? (a->f < b->f ? -1 : a->f > b->f) : memcmp (value_str (a, width), value_str (b, width), width)); } @@ -112,8 +115,8 @@ value_compare_3way (const union value *a, const union value *b, int width) bool value_equal (const union value *a, const union value *b, int width) { - return (width == 0 - ? a->f == b->f + return (width == -1 ? true + : width == 0 ? a->f == b->f : !memcmp (value_str (a, width), value_str (b, width), width)); } @@ -122,8 +125,8 @@ value_equal (const union value *a, const union value *b, int width) unsigned int value_hash (const union value *value, int width, unsigned int basis) { - return (width == 0 - ? hash_double (value->f, basis) + return (width == -1 ? basis + : width == 0 ? hash_double (value->f, basis) : hash_bytes (value_str (value, width), width, basis)); }