fix: terminate XIM UTF-8 text runs

This commit is contained in:
2026-08-12 16:43:57 +09:00
parent 71af269efe
commit 4e8720a40d
3 changed files with 48 additions and 12 deletions

View File

@@ -4,25 +4,39 @@
#include <xcb-imdkit/encoding.h>
#include "ximtext.h"
char*
ximutf8compound(const char *s, size_t len, size_t *outlen)
{
static const char begin[] = "\033%G";
static const char end[] = "\033%@";
char *ct;
size_t n;
if(s == NULL || len > SIZE_MAX - (sizeof begin + sizeof end - 1))
return NULL;
n = len + sizeof begin + sizeof end - 2;
ct = malloc(n + 1);
if(ct == NULL)
return NULL;
memcpy(ct, begin, sizeof begin - 1);
memcpy(ct + sizeof begin - 1, s, len);
memcpy(ct + sizeof begin - 1 + len, end, sizeof end - 1);
ct[n] = '\0';
if(outlen != NULL)
*outlen = n;
return ct;
}
char*
ximcompound(const char *s, size_t len, size_t *outlen)
{
static const char utf8[] = "\033%G";
char *ct;
if(s == NULL || len > SIZE_MAX - sizeof utf8)
if(s == NULL)
return NULL;
ct = xcb_utf8_to_compound_text(s, len, outlen);
if(ct != NULL)
return ct;
/* X11 Compound Text reserves ESC % G for an embedded UTF-8 run. */
ct = malloc(len + sizeof utf8);
if(ct == NULL)
return NULL;
memcpy(ct, utf8, sizeof utf8 - 1);
memcpy(ct + sizeof utf8 - 1, s, len);
ct[len + sizeof utf8 - 1] = '\0';
if(outlen != NULL)
*outlen = len + sizeof utf8 - 1;
return ct;
/* X.Org Compound Text extension for characters in no legacy charset. */
return ximutf8compound(s, len, outlen);
}

View File

@@ -1,3 +1,4 @@
#include <stddef.h>
char *ximcompound(const char*, size_t, size_t*);
char *ximutf8compound(const char*, size_t, size_t*);