From d4a1283797b5a964d92e64c698d65f701a90e1cc Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Wed, 11 Sep 2024 20:13:30 +0100 Subject: xsnprintf: various improvements related to xvsnprintf() and xsnprintf() Summary of changes: * Make xvsnprintf() static * restrict-qualify pointer arguments (as done by the libc equivalents) * Make comments and spec references more thorough * Remove pointless `n <= INT_MAX` assertion (see comment) * Use FATAL_ERROR() instead of xassert() (since the assertion is inside a shared util function but the caller is responsible for ensuring the condition holds true) * Change some callers to use size_t instead of int for the return value (negative returns are impossible and all subsequent uses are size_t) The updated comments and code were taken (and adapted) from: https://gitlab.com/craigbarnes/dte/-/blob/49260bb154bca0434462a41c061e512540ec2e49/src/util/xsnprintf.c#L6-50 This work was entirely authored by me and I hereby license this contribution under the MIT license (stated explicitly, so that there's no ambiguity w.r.t. the original license). --- dcs.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'dcs.c') diff --git a/dcs.c b/dcs.c index 6a832aed..7518c07c 100644 --- a/dcs.c +++ b/dcs.c @@ -271,7 +271,7 @@ decrqss_unhook(struct terminal *term) if (n == 1 && query[0] == 'r') { /* DECSTBM - Set Top and Bottom Margins */ char reply[64]; - int len = xsnprintf(reply, sizeof(reply), "\033P1$r%d;%dr\033\\", + size_t len = xsnprintf(reply, sizeof(reply), "\033P1$r%d;%dr\033\\", term->scroll_region.start + 1, term->scroll_region.end); term_to_slave(term, reply, len); @@ -300,7 +300,7 @@ decrqss_unhook(struct terminal *term) if (a->underline) { if (term->vt.underline.style > UNDERLINE_SINGLE) { char value[4]; - int val_len = + size_t val_len = xsnprintf(value, sizeof(value), "4:%d", term->vt.underline.style); append_sgr_attr_n(&reply, &len, value, val_len); } else @@ -321,7 +321,7 @@ decrqss_unhook(struct terminal *term) case COLOR_BASE16: { char value[4]; - int val_len = xsnprintf( + size_t val_len = xsnprintf( value, sizeof(value), "%u", a->fg >= 8 ? a->fg - 8 + 90 : a->fg + 30); append_sgr_attr_n(&reply, &len, value, val_len); @@ -330,7 +330,7 @@ decrqss_unhook(struct terminal *term) case COLOR_BASE256: { char value[16]; - int val_len = xsnprintf(value, sizeof(value), "38:5:%u", a->fg); + size_t val_len = xsnprintf(value, sizeof(value), "38:5:%u", a->fg); append_sgr_attr_n(&reply, &len, value, val_len); break; } @@ -341,7 +341,7 @@ decrqss_unhook(struct terminal *term) uint8_t b = a->fg >> 0; char value[32]; - int val_len = xsnprintf( + size_t val_len = xsnprintf( value, sizeof(value), "38:2::%hhu:%hhu:%hhu", r, g, b); append_sgr_attr_n(&reply, &len, value, val_len); break; @@ -354,7 +354,7 @@ decrqss_unhook(struct terminal *term) case COLOR_BASE16: { char value[4]; - int val_len = xsnprintf( + size_t val_len = xsnprintf( value, sizeof(value), "%u", a->bg >= 8 ? a->bg - 8 + 100 : a->bg + 40); append_sgr_attr_n(&reply, &len, value, val_len); @@ -363,7 +363,7 @@ decrqss_unhook(struct terminal *term) case COLOR_BASE256: { char value[16]; - int val_len = xsnprintf(value, sizeof(value), "48:5:%u", a->bg); + size_t val_len = xsnprintf(value, sizeof(value), "48:5:%u", a->bg); append_sgr_attr_n(&reply, &len, value, val_len); break; } @@ -374,7 +374,7 @@ decrqss_unhook(struct terminal *term) uint8_t b = a->bg >> 0; char value[32]; - int val_len = xsnprintf( + size_t val_len = xsnprintf( value, sizeof(value), "48:2::%hhu:%hhu:%hhu", r, g, b); append_sgr_attr_n(&reply, &len, value, val_len); break; @@ -388,7 +388,7 @@ decrqss_unhook(struct terminal *term) case COLOR_BASE256: { char value[16]; - int val_len = xsnprintf( + size_t val_len = xsnprintf( value, sizeof(value), "58:5:%u", term->vt.underline.color); append_sgr_attr_n(&reply, &len, value, val_len); break; @@ -400,7 +400,7 @@ decrqss_unhook(struct terminal *term) uint8_t b = term->vt.underline.color >> 0; char value[32]; - int val_len = xsnprintf( + size_t val_len = xsnprintf( value, sizeof(value), "58:2::%hhu:%hhu:%hhu", r, g, b); append_sgr_attr_n(&reply, &len, value, val_len); break; @@ -432,7 +432,7 @@ decrqss_unhook(struct terminal *term) mode--; char reply[16]; - int len = xsnprintf(reply, sizeof(reply), "\033P1$r%d q\033\\", mode); + size_t len = xsnprintf(reply, sizeof(reply), "\033P1$r%d q\033\\", mode); term_to_slave(term, reply, len); } -- cgit v1.3