From: Thayne McCombs <astrotha...@gmail.com> A couple of functions in sample.c have almost identical code for updating the samples string buffer. This adds a new helper function to do this and uses it in both places. --- include/haproxy/buf.h | 22 ++++++++++++++++++++++ src/sample.c | 29 ++--------------------------- 2 files changed, 24 insertions(+), 27 deletions(-)
diff --git a/include/haproxy/buf.h b/include/haproxy/buf.h index 4ea4b73f1..303635c39 100644 --- a/include/haproxy/buf.h +++ b/include/haproxy/buf.h @@ -940,6 +940,28 @@ static inline int b_peek_varint(struct buffer *b, size_t ofs, uint64_t *vptr) return size; } +/* + * b_set_area_sub(): Replace the current buffer with a sub-slice of the + * current buffer. Sets <area> to <new_area>, <data> to <new_data>, and + * <size> to the new size accounting for the change to area. The range from + * <new_area> to <new_area + new_data> must be within the range of the current buffer. + */ +static inline void b_set_area_sub(struct buffer *b, char *new_area, size_t new_data) +{ + BUG_ON_HOT(new_area < b->area); + BUG_ON_HOT(new_area + new_data > b->area + b->data); + + b->data = new_data; + /* If buffer is len 0, no need to + change pointers or to update size */ + if (!new_data) + return; + /* Compute remaining size if needed */ + if (b->size) + b->size -= new_area - b->area; + b-> area = new_area; +} + /* * Buffer ring management. diff --git a/src/sample.c b/src/sample.c index 50ae76b6e..237b88056 100644 --- a/src/sample.c +++ b/src/sample.c @@ -2490,19 +2490,7 @@ static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void * return 0; } found: - smp->data.u.str.data = end - start; - /* If ret string is len 0, no need to - change pointers or to update size */ - if (!smp->data.u.str.data) - return 1; - - /* Compute remaining size if needed - Note: smp->data.u.str.size cannot be set to 0 */ - if (smp->data.u.str.size) - smp->data.u.str.size -= start - smp->data.u.str.area; - - smp->data.u.str.area = start; - + b_set_area_sub(&(smp->data.u.str), start, end - start); return 1; } @@ -2590,20 +2578,7 @@ static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *p return 1; } found: - smp->data.u.str.data = end - start; - /* If ret string is len 0, no need to - change pointers or to update size */ - if (!smp->data.u.str.data) - return 1; - - - /* Compute remaining size if needed - Note: smp->data.u.str.size cannot be set to 0 */ - if (smp->data.u.str.size) - smp->data.u.str.size -= start - smp->data.u.str.area; - - smp->data.u.str.area = start; - + b_set_area_sub(&(smp->data.u.str), start, end - start); return 1; } -- 2.36.1