From 1956dd44c25567c35a951757f24763b8e51205ce Mon Sep 17 00:00:00 2001 From: Brad Knotwell Date: Thu, 9 Mar 2023 21:55:40 -0800 Subject: [PATCH] Validate enumerations. --- libasn1compiler/asn1c_C.c | 32 ++++++++++++++++++++++---------- libasn1compiler/asn1c_internal.h | 3 +++ libasn1compiler/asn1compiler.c | 4 ++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/libasn1compiler/asn1c_C.c b/libasn1compiler/asn1c_C.c index 470f38cf3..3f11cbaea 100644 --- a/libasn1compiler/asn1c_C.c +++ b/libasn1compiler/asn1c_C.c @@ -196,7 +196,21 @@ asn1c_lang_C_type_common_INTEGER(arg_t *arg) { if(map_extensions) OUT("\t/* This list is extensible */\n"); OUT("};\n"); - + OUT("static int asn_validate_%s(const asn_TYPE_descriptor_t *td,\n", MKID(expr)); + OUT(" const void *sptr,\n"); + OUT(" asn_app_constraint_failed_f *ctfailcb,\n"); + OUT(" void* app_key) {\n"); + OUT(" if(! sptr) { return -1; }\n"); + OUT(" e_%s value = *(e_%s*)sptr;\n", MKID(expr), MKID(expr)); + OUT(" switch(value) {\n"); + for(eidx = 0; eidx < el_count; eidx++) { + OUT(" case %s_%s:\n", MKID(expr), v2e[eidx].name); + } + OUT(" return 0;\n"); + OUT(" }\n"); + OUT(" return -1;\n"); + OUT("}\n"); + arg->param.localvalidation = 1; OUT("static const unsigned int asn_MAP_%s_enum2value_%d[] = {\n", MKID(expr), expr->_type_unique_index); qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName); @@ -3185,18 +3199,16 @@ emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode, int tags_ OUT("_" #foo ""); \ } while(0) -#define FUNCREF2(foo) \ - do { \ - OUT("%s", p2); \ - OUT("_" #foo); \ - } while(0) - if (arg->flags & A1C_NO_CONSTRAINTS) { OUT("0"); } else { - if (!expr->combined_constraints) - FUNCREF2(constraint); - else + if (!expr->combined_constraints) { + if(arg->param.localvalidation == 1) + OUT("asn_validate_%s", p); + else + OUT("%s_constraint", p2); + arg->param.localvalidation = 0; + } else FUNCREF(constraint); } OUT("\n"); diff --git a/libasn1compiler/asn1c_internal.h b/libasn1compiler/asn1c_internal.h index 7a07e3540..8c19f2c27 100644 --- a/libasn1compiler/asn1c_internal.h +++ b/libasn1compiler/asn1c_internal.h @@ -60,6 +60,9 @@ typedef struct arg_s { asn1p_expr_t *expr; int embed; + union { + int localvalidation; + } param; } arg_t; /* diff --git a/libasn1compiler/asn1compiler.c b/libasn1compiler/asn1compiler.c index c3741bc63..257550d33 100644 --- a/libasn1compiler/asn1compiler.c +++ b/libasn1compiler/asn1compiler.c @@ -12,7 +12,7 @@ static int asn1c_detach_streams(asn1p_expr_t *expr); int asn1_compile(asn1p_t *asn, const char *datadir, const char *destdir, enum asn1c_flags flags, int argc, int optc, char **argv) { - arg_t arg_s; + arg_t arg_s = {0}; arg_t *arg = &arg_s; asn1p_module_t *mod; int ret; @@ -242,7 +242,7 @@ asn1c_debug_expr_naming(arg_t *arg) { void asn1c_debug_type_naming(asn1p_t *asn, enum asn1c_flags flags, char **asn_type_names) { - arg_t arg_s; + arg_t arg_s = {0}; arg_t *arg = &arg_s; asn1p_module_t *mod;