From 4a69dd403accbc3e810d0b851f7e726ac016384e Mon Sep 17 00:00:00 2001 From: Consolatis <35009135+Consolatis@users.noreply.github.com> Date: Sat, 16 Mar 2024 20:04:11 +0100 Subject: [PATCH] src/common/buf.c: add private buf_replace_by() helper ..to reduce code duplication --- src/common/buf.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/common/buf.c b/src/common/buf.c index 37505d5f..8342b689 100644 --- a/src/common/buf.c +++ b/src/common/buf.c @@ -5,6 +5,15 @@ #include "common/buf.h" #include "common/mem.h" +static void +buf_replace_by(struct buf *dst, struct buf *src) +{ + free(dst->buf); + dst->buf = src->buf; + dst->len = src->len; + dst->alloc = src->alloc; +} + void buf_expand_tilde(struct buf *s) { @@ -17,10 +26,7 @@ buf_expand_tilde(struct buf *s) buf_add_char(&new, s->buf[i]); } } - free(s->buf); - s->buf = new.buf; - s->len = new.len; - s->alloc = new.alloc; + buf_replace_by(s, &new); } static void @@ -70,10 +76,7 @@ buf_expand_shell_variables(struct buf *s) } } buf_finish(&environment_variable); - free(s->buf); - s->buf = new.buf; - s->len = new.len; - s->alloc = new.alloc; + buf_replace_by(s, &new); } void