From 0227bd67d17068f64400d4dcb02bc9341792d519 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 13 Sep 2026 23:37:30 -0700 Subject: [PATCH] sed: deal with musl-like mbrtoc32 behavior * sed/sed.h (c32rtomb1): When mbrtoc32 acts like musl (because Gnulib has replaced it) but c32rtomb does not (because Gnulib does not replace it), convert back to the byte by hand. --- sed/sed.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/sed/sed.h b/sed/sed.h index a520955..6e8ce06 100644 --- a/sed/sed.h +++ b/sed/sed.h @@ -291,13 +291,22 @@ extern bool sandbox; /* If set, print debugging information. */ extern bool debug; -/* Convert to S the character CH. +/* Convert to S the character CH. S must point to at least MB_LEN_MAX bytes. Return the number of bytes in S's representation. */ SED_INLINE idx_t c32rtomb1 (char *s, char32_t ch) { mbstate_t mbs; mbszero (&mbs); - return c32rtomb (s, ch, &mbs); + size_t nbytes = c32rtomb (s, ch, &mbs); + if (nbytes == (size_t) {-1}) + { + /* This can happen if the Gnulib mbrtoc32 is in use in the C locale, + and has converted (e.g.) '\xE8' to 0xDFE8. Translate it back + by assigning, which keeps only the low 8 bits. */ + *s = ch; + nbytes = 1; + } + return nbytes; } /* Use this to suppress gcc's '...may be used before initialized' warnings. */