xkb: Convert more sprintf calls to snprintf in xkbtext.c

Based on xorg/lib/libxkbfile@390acfe5bb88cdab509b5eaae4041f265e969d2b

Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/1821>
This commit is contained in:
Alan Coopersmith 2025-02-22 16:15:01 -08:00 committed by Marge Bot
parent 6d33834186
commit 60419d8e4a

View File

@ -150,11 +150,12 @@ XkbVModMaskText(XkbDescPtr xkb,
char *str, buf[VMOD_BUFFER_SIZE]; char *str, buf[VMOD_BUFFER_SIZE];
if ((modMask == 0) && (mask == 0)) { if ((modMask == 0) && (mask == 0)) {
rtrn = tbGetBuffer(5); const int rtrnsize = 5;
rtrn = tbGetBuffer(rtrnsize);
if (format == XkbCFile) if (format == XkbCFile)
sprintf(rtrn, "0"); snprintf(rtrn, rtrnsize, "0");
else else
sprintf(rtrn, "none"); snprintf(rtrn, rtrnsize, "none");
return rtrn; return rtrn;
} }
if (modMask != 0) if (modMask != 0)
@ -305,8 +306,9 @@ XkbModMaskText(unsigned mask, unsigned format)
XkbConfigText(unsigned config, unsigned format) XkbConfigText(unsigned config, unsigned format)
{ {
static char *buf; static char *buf;
const int bufsize = 32;
buf = tbGetBuffer(32); buf = tbGetBuffer(bufsize);
switch (config) { switch (config) {
case XkmSemanticsFile: case XkmSemanticsFile:
strcpy(buf, "Semantics"); strcpy(buf, "Semantics");
@ -340,7 +342,7 @@ XkbConfigText(unsigned config, unsigned format)
strcpy(buf, "VirtualMods"); strcpy(buf, "VirtualMods");
break; break;
default: default:
sprintf(buf, "unknown(%d)", config); snprintf(buf, bufsize, "unknown(%d)", config);
break; break;
} }
return buf; return buf;
@ -439,7 +441,7 @@ static const char *imWhichNames[] = {
char * char *
XkbIMWhichStateMaskText(unsigned use_which, unsigned format) XkbIMWhichStateMaskText(unsigned use_which, unsigned format)
{ {
int len; int len, bufsize;
unsigned i, bit, tmp; unsigned i, bit, tmp;
char *buf; char *buf;
@ -457,7 +459,8 @@ XkbIMWhichStateMaskText(unsigned use_which, unsigned format)
len += 9; len += 9;
} }
} }
buf = tbGetBuffer(len + 1); bufsize = len + 1;
buf = tbGetBuffer(bufsize);
tmp = use_which & XkbIM_UseAnyMods; tmp = use_which & XkbIM_UseAnyMods;
for (len = i = 0, bit = 1; tmp != 0; i++, bit <<= 1) { for (len = i = 0, bit = 1; tmp != 0; i++, bit <<= 1) {
if (tmp & bit) { if (tmp & bit) {
@ -465,13 +468,14 @@ XkbIMWhichStateMaskText(unsigned use_which, unsigned format)
if (format == XkbCFile) { if (format == XkbCFile) {
if (len != 0) if (len != 0)
buf[len++] = '|'; buf[len++] = '|';
sprintf(&buf[len], "XkbIM_Use%s", imWhichNames[i]); snprintf(&buf[len], bufsize - len,
"XkbIM_Use%s", imWhichNames[i]);
buf[len + 9] = toupper((unsigned char)buf[len + 9]); buf[len + 9] = toupper((unsigned char)buf[len + 9]);
} }
else { else {
if (len != 0) if (len != 0)
buf[len++] = '+'; buf[len++] = '+';
sprintf(&buf[len], "%s", imWhichNames[i]); snprintf(&buf[len], bufsize - len, "%s", imWhichNames[i]);
} }
len += strlen(&buf[len]); len += strlen(&buf[len]);
} }
@ -618,18 +622,19 @@ XkbGeomFPText(int val, unsigned format)
{ {
int whole, frac; int whole, frac;
char *buf; char *buf;
const int bufsize = 12;
buf = tbGetBuffer(12); buf = tbGetBuffer(bufsize);
if (format == XkbCFile) { if (format == XkbCFile) {
sprintf(buf, "%d", val); snprintf(buf, bufsize, "%d", val);
} }
else { else {
whole = val / XkbGeomPtsPerMM; whole = val / XkbGeomPtsPerMM;
frac = val % XkbGeomPtsPerMM; frac = val % XkbGeomPtsPerMM;
if (frac != 0) if (frac != 0)
sprintf(buf, "%d.%d", whole, frac); snprintf(buf, bufsize, "%d.%d", whole, frac);
else else
sprintf(buf, "%d", whole); snprintf(buf, bufsize, "%d", whole);
} }
return buf; return buf;
} }
@ -640,7 +645,8 @@ XkbDoodadTypeText(unsigned type, unsigned format)
char *buf; char *buf;
if (format == XkbCFile) { if (format == XkbCFile) {
buf = tbGetBuffer(24); const int bufsize = 24;
buf = tbGetBuffer(bufsize);
if (type == XkbOutlineDoodad) if (type == XkbOutlineDoodad)
strcpy(buf, "XkbOutlineDoodad"); strcpy(buf, "XkbOutlineDoodad");
else if (type == XkbSolidDoodad) else if (type == XkbSolidDoodad)
@ -652,10 +658,11 @@ XkbDoodadTypeText(unsigned type, unsigned format)
else if (type == XkbLogoDoodad) else if (type == XkbLogoDoodad)
strcpy(buf, "XkbLogoDoodad"); strcpy(buf, "XkbLogoDoodad");
else else
sprintf(buf, "UnknownDoodad%d", type); snprintf(buf, bufsize, "UnknownDoodad%d", type);
} }
else { else {
buf = tbGetBuffer(12); const int bufsize = 12;
buf = tbGetBuffer(bufsize);
if (type == XkbOutlineDoodad) if (type == XkbOutlineDoodad)
strcpy(buf, "outline"); strcpy(buf, "outline");
else if (type == XkbSolidDoodad) else if (type == XkbSolidDoodad)
@ -667,7 +674,7 @@ XkbDoodadTypeText(unsigned type, unsigned format)
else if (type == XkbLogoDoodad) else if (type == XkbLogoDoodad)
strcpy(buf, "logo"); strcpy(buf, "logo");
else else
sprintf(buf, "unknown%d", type); snprintf(buf, bufsize, "unknown%d", type);
} }
return buf; return buf;
} }
@ -1264,6 +1271,7 @@ XkbBehaviorText(XkbDescPtr xkb, XkbBehavior * behavior, unsigned format)
} }
else if (type == XkbKB_RadioGroup) { else if (type == XkbKB_RadioGroup) {
int g; int g;
size_t tmpsize;
g = ((behavior->data) & (~XkbKB_RGAllowNone)) + 1; g = ((behavior->data) & (~XkbKB_RGAllowNone)) + 1;
if (XkbKB_RGAllowNone & behavior->data) { if (XkbKB_RGAllowNone & behavior->data) {
@ -1272,10 +1280,11 @@ XkbBehaviorText(XkbDescPtr xkb, XkbBehavior * behavior, unsigned format)
} }
else else
tmp = buf; tmp = buf;
tmpsize = sizeof(buf) - (tmp - buf);
if (permanent) if (permanent)
sprintf(tmp, "permanentRadioGroup= %d", g); snprintf(tmp, tmpsize, "permanentRadioGroup= %d", g);
else else
sprintf(tmp, "radioGroup= %d", g); snprintf(tmp, tmpsize, "radioGroup= %d", g);
} }
else if ((type == XkbKB_Overlay1) || (type == XkbKB_Overlay2)) { else if ((type == XkbKB_Overlay1) || (type == XkbKB_Overlay2)) {
int ndx, kc; int ndx, kc;