From 0bcb0478060be52d2bc9c26a3e88bb759f890d74 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 4 Mar 2008 07:55:32 +0100 Subject: [PATCH] Make nstr_exec_val() more robust Oops on bad argument, and make a better error value then. --- include/nsc.h | 3 +++ src/lib/common/nstreval.c | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/include/nsc.h b/include/nsc.h index 91519206..636e0548 100644 --- a/include/nsc.h +++ b/include/nsc.h @@ -67,6 +67,9 @@ typedef enum { } nsc_type; typedef char packed_nsc_type; +/* Is TYPE a promoted value type? */ +#define NSC_IS_PROMOTED(type) (NSC_LONG <= (type) && (type) <= NSC_STRING) + /* Return nsc_type for a signed integer with the same size as TYPE. */ #define NSC_SITYPE(type) \ (sizeof(type) == 1 ? NSC_CHAR \ diff --git a/src/lib/common/nstreval.c b/src/lib/common/nstreval.c index c730ff5a..24d9dda0 100644 --- a/src/lib/common/nstreval.c +++ b/src/lib/common/nstreval.c @@ -36,6 +36,7 @@ #include #include +#include #include "file.h" #include "nat.h" #include "nsc.h" @@ -45,7 +46,7 @@ /* * Evaluate VAL. * If VAL is symbolic, evaluate it into a promoted value type. - * Use coordinate system of country CNUM. + * Use country CNUM's coordinate system and access control. * PTR points to a context object of the type that was used to compile * the value. * Unless WANT is NSC_NOTYPE, coerce the value to promoted value type @@ -60,10 +61,10 @@ nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want) int idx; struct natstr *natp; + if (CANT_HAPPEN(want != NSC_NOTYPE && !NSC_IS_PROMOTED(want))) + want = nstr_promote(want); + switch (val->val_cat) { - default: - CANT_REACH(); - /* fall through */ case NSC_VAL: valtype = val->val_type; break; @@ -134,6 +135,10 @@ nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want) val->val_as.lng = 0; } val->val_cat = NSC_VAL; + break; + default: + CANT_REACH(); + valtype = NSC_NOTYPE; } if (valtype == want) @@ -147,14 +152,9 @@ nstr_exec_val(struct valstr *val, natid cnum, void *ptr, nsc_type want) CANT_REACH(); /* FIXME implement */ if (CANT_HAPPEN(valtype != want && want != NSC_NOTYPE)) { + /* make up an error value */ valtype = want; - switch (want) { - case NSC_LONG: val->val_as.lng = 0; break; - case NSC_DOUBLE: val->val_as.dbl = 0.0; break; - case NSC_STRING: val->val_as.str.base = NULL; break; - default: - CANT_REACH(); - } + memset(&val->val_as, 0, sizeof(val->val_as)); } val->val_type = valtype;