Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d.labels: Fix buffer overflow issues in do_labels.c #4041

Merged
merged 17 commits into from
Jul 26, 2024
Merged
32 changes: 25 additions & 7 deletions display/d.labels/do_labels.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#define YES 1
#define NO 0

#define BUFFSIZE 128
#define FONTSIZE 256
#define WORDSIZE 50

static double east;
static double north;
static int xoffset;
Expand Down Expand Up @@ -68,6 +72,13 @@ int initialize_options(void)
int do_labels(FILE *infile, int do_rotation)
{
char buff[128];
ShubhamDesai marked this conversation as resolved.
Show resolved Hide resolved
char buff_fmt[10];
char font_fmt[10];
char word_fmt[10];
nilason marked this conversation as resolved.
Show resolved Hide resolved

snprintf(buff_fmt, sizeof(buff_fmt), "%%%ds", BUFFSIZE - 1);
snprintf(font_fmt, sizeof(font_fmt), "%%%ds", FONTSIZE - 1);
nilason marked this conversation as resolved.
Show resolved Hide resolved
snprintf(word_fmt, sizeof(word_fmt), "%%%ds%%%ds", WORDSIZE - 1, WORDSIZE - 1);

initialize_options();

Expand All @@ -84,7 +95,7 @@ int do_labels(FILE *infile, int do_rotation)
else if (!strncmp(text, "yof", 3))
sscanf(text, "%*s %d", &yoffset);
else if (!strncmp(text, "col", 3)) {
sscanf(text, "%*s %s", buff);
sscanf(text, buff_fmt, buff);
set_RGBA_from_str(&color, buff);
}
else if (!strncmp(text, "siz", 3))
Expand All @@ -94,15 +105,15 @@ int do_labels(FILE *infile, int do_rotation)
else if (!strncmp(text, "wid", 3))
sscanf(text, "%*s %lf", &width);
else if (!strncmp(text, "bac", 3)) {
sscanf(text, "%*s %s", buff);
sscanf(text, buff_fmt, buff);
set_RGBA_from_str(&background, buff);
}
else if (!strncmp(text, "bor", 3)) {
sscanf(text, "%*s %s", buff);
sscanf(text, buff_fmt, buff);
set_RGBA_from_str(&border, buff);
}
else if (!strncmp(text, "opa", 3)) {
sscanf(text, "%*s %s", buff);
sscanf(text, buff_fmt, buff);
if (!strncmp(buff, "YES", 3))
opaque = YES;
else
Expand All @@ -115,15 +126,16 @@ int do_labels(FILE *infile, int do_rotation)
}
}
else if (!strncmp(text, "fon", 3)) {
if (sscanf(text, "%*s %s", font) != 1 || !strcmp(font, "standard"))
if (sscanf(text, font_fmt, font) != 1 || !strcmp(font, "standard"))

ShubhamDesai marked this conversation as resolved.
Show resolved Hide resolved
strcpy(font, std_font);
}
else if (!strncmp(text, "rot", 3)) {
if (do_rotation)
sscanf(text, "%*s %lf", &rotation);
}
else if (!strncmp(text, "hco", 3)) {
sscanf(text, "%*s %s", buff);
sscanf(text, buff_fmt, buff);
set_RGBA_from_str(&highlight_color, buff);
}
else if (!strncmp(text, "hwi", 3))
Expand Down Expand Up @@ -452,7 +464,10 @@ int scan_ref(char *buf)
if (buf[i] >= 'A' && buf[i] <= 'Z')
buf[i] += 'a' - 'A';
xref = yref = CENT;
switch (sscanf(buf, "%s%s", word1, word2)) {
char word_fmt[10];
snprintf(word_fmt, sizeof(word_fmt), "%%%ds%%%ds", WORDSIZE - 1, WORDSIZE - 1);

ShubhamDesai marked this conversation as resolved.
Show resolved Hide resolved
switch (sscanf(buf, word_fmt, word1, word2)) {
case 2:
if (!(xmatch(word2) || ymatch(word2)))
return 0;
Expand All @@ -461,8 +476,11 @@ int scan_ref(char *buf)
if (xmatch(word1) || ymatch(word1))
return 1;
FALLTHROUGH;
case EOF:
FALLTHROUGH;
nilason marked this conversation as resolved.
Show resolved Hide resolved
default:
return 0;

}
}

Expand Down
Loading