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:

49260bb154/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).
This commit is contained in:
Craig Barnes 2024-09-11 20:13:30 +01:00 committed by Daniel Eklöf
parent 31f88e636c
commit d4a1283797
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
8 changed files with 54 additions and 35 deletions

22
dcs.c
View file

@ -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);
}