From: Willy Tarreau Date: Wed, 6 Oct 2021 13:29:00 +0000 (+0200) Subject: MINOR: sample: provide a generic var-to-sample conversion function X-Git-Tag: v2.5-dev9~109 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=168e8de1d06adc7aa3e7e2cc2a36935a77c79b9c;p=thirdparty%2Fhaproxy.git MINOR: sample: provide a generic var-to-sample conversion function We're using variable-to-sample conversion at least 4 times in the code, two of which are bogus. Let's introduce a generic conversion function that performs the required checks. --- diff --git a/include/haproxy/sample.h b/include/haproxy/sample.h index 5f4a6cb086..670205f03c 100644 --- a/include/haproxy/sample.h +++ b/include/haproxy/sample.h @@ -40,6 +40,7 @@ struct sample *sample_process(struct proxy *px, struct session *sess, struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt, struct sample_expr *expr, int smp_type); +int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type); int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp); int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp); void release_sample_expr(struct sample_expr *expr); diff --git a/src/sample.c b/src/sample.c index 67853d2960..7cee16c34b 100644 --- a/src/sample.c +++ b/src/sample.c @@ -1630,6 +1630,24 @@ static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp return 1; } +/* This function returns a sample struct filled with the conversion of variable + * to sample type (SMP_T_*), via a cast to the target type. If the + * variable cannot be retrieved or casted, 0 is returned, otherwise 1. + * + * Keep in mind that the sample content may be written to a pre-allocated + * trash chunk as returned by get_trash_chunk(). + */ +int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type) +{ + if (!vars_get_by_desc(var, smp, NULL)) + return 0; + if (!sample_casts[smp->data.type][type]) + return 0; + if (!sample_casts[smp->data.type][type](smp)) + return 0; + return 1; +} + static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private) { blk_SHA_CTX ctx;