xfree86: parser: check for alloc failure and possible NULL pointers

Adding paranoid extra checks against allocation failure and NULL pointers.
Even though might not be actually hit in practise, it's still better to
be cautious, just in case. And reducing analyzer noise this way.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-05-07 14:58:32 +02:00
parent 88d561bf18
commit b28285332e
6 changed files with 25 additions and 8 deletions

View File

@ -55,6 +55,8 @@
#include <xorg-config.h> #include <xorg-config.h>
#endif #endif
#include <assert.h>
#include <X11/Xos.h> #include <X11/Xos.h>
#include "xf86Parser.h" #include "xf86Parser.h"
#include "xf86tokens.h" #include "xf86tokens.h"
@ -109,9 +111,9 @@ xf86parseFilesSection(void)
} }
} }
ptr->file_fontpath = realloc(ptr->file_fontpath, i); ptr->file_fontpath = realloc(ptr->file_fontpath, i);
assert(ptr->file_fontpath);
if (j) if (j)
strcat(ptr->file_fontpath, ","); strcat(ptr->file_fontpath, ",");
strcat(ptr->file_fontpath, str); strcat(ptr->file_fontpath, str);
free(xf86_lex_val.str); free(xf86_lex_val.str);
break; break;
@ -121,7 +123,8 @@ xf86parseFilesSection(void)
l = FALSE; l = FALSE;
str = xf86_lex_val.str; str = xf86_lex_val.str;
if (ptr->file_modulepath == NULL) { if (ptr->file_modulepath == NULL) {
ptr->file_modulepath = malloc(1); ptr->file_modulepath = calloc(1, 1);
assert(ptr->file_modulepath);
ptr->file_modulepath[0] = '\0'; ptr->file_modulepath[0] = '\0';
k = strlen(str) + 1; k = strlen(str) + 1;
} }
@ -134,6 +137,7 @@ xf86parseFilesSection(void)
} }
} }
ptr->file_modulepath = realloc(ptr->file_modulepath, k); ptr->file_modulepath = realloc(ptr->file_modulepath, k);
assert(ptr->file_modulepath);
if (l) if (l)
strcat(ptr->file_modulepath, ","); strcat(ptr->file_modulepath, ",");

View File

@ -55,6 +55,8 @@
#include <xorg-config.h> #include <xorg-config.h>
#endif #endif
#include <assert.h>
#include "xf86Parser.h" #include "xf86Parser.h"
#include "xf86tokens.h" #include "xf86tokens.h"
#include "Configint.h" #include "Configint.h"
@ -200,6 +202,7 @@ addNewOption2(XF86OptionPtr head, char *name, char *_val, int used)
} }
else else
new = calloc(1, sizeof(*new)); new = calloc(1, sizeof(*new));
assert(new);
new->opt_name = name; new->opt_name = name;
new->opt_val = _val; new->opt_val = _val;
new->opt_used = used; new->opt_used = used;
@ -437,6 +440,7 @@ xf86parseOption(XF86OptionPtr head)
name = xf86_lex_val.str; name = xf86_lex_val.str;
if ((token = xf86getSubToken(&comment)) == STRING) { if ((token = xf86getSubToken(&comment)) == STRING) {
option = xf86newOption(name, xf86_lex_val.str); option = xf86newOption(name, xf86_lex_val.str);
assert(option);
option->opt_comment = comment; option->opt_comment = comment;
if ((token = xf86getToken(NULL)) == COMMENT) { if ((token = xf86getToken(NULL)) == COMMENT) {
option->opt_comment = xf86addComment(option->opt_comment, xf86_lex_val.str); option->opt_comment = xf86addComment(option->opt_comment, xf86_lex_val.str);
@ -448,6 +452,7 @@ xf86parseOption(XF86OptionPtr head)
} }
else { else {
option = xf86newOption(name, NULL); option = xf86newOption(name, NULL);
assert(option);
option->opt_comment = comment; option->opt_comment = comment;
if (token == COMMENT) { if (token == COMMENT) {
option->opt_comment = xf86addComment(option->opt_comment, xf86_lex_val.str); option->opt_comment = xf86addComment(option->opt_comment, xf86_lex_val.str);

View File

@ -450,9 +450,9 @@ xf86layoutAddInputDevices(XF86ConfigPtr config, XF86ConfLayoutPtr layout)
} }
if (!iref) { if (!iref) {
XF86ConfInputrefPtr iptr; XF86ConfInputrefPtr iptr = calloc(1, sizeof(XF86ConfInputrefRec));
if (!iptr)
iptr = calloc(1, sizeof(XF86ConfInputrefRec)); return -1;
iptr->iref_inputdev_str = input->inp_identifier; iptr->iref_inputdev_str = input->inp_identifier;
layout->lay_input_lst = (XF86ConfInputrefPtr) layout->lay_input_lst = (XF86ConfInputrefPtr)
xf86addListItem((glp) layout->lay_input_lst, (glp) iptr); xf86addListItem((glp) layout->lay_input_lst, (glp) iptr);

View File

@ -56,6 +56,8 @@
#include <xorg-config.h> #include <xorg-config.h>
#endif #endif
#include <assert.h>
#include "xf86Parser.h" #include "xf86Parser.h"
#include "xf86tokens.h" #include "xf86tokens.h"
#include "Configint.h" #include "Configint.h"
@ -228,6 +230,7 @@ xf86addNewLoadDirective(XF86LoadPtr head, const char *name, int type,
int token; int token;
new = calloc(1, sizeof(XF86LoadRec)); new = calloc(1, sizeof(XF86LoadRec));
assert(new);
new->load_name = name; new->load_name = name;
new->load_type = type; new->load_type = type;
new->load_opt = opts; new->load_opt = opts;

View File

@ -58,9 +58,12 @@ xf86freeOutputClassList(XF86ConfOutputClassPtr ptr)
xorg_list_for_each_entry_safe(group, next, &ptr->match_driver, entry) { xorg_list_for_each_entry_safe(group, next, &ptr->match_driver, entry) {
xorg_list_del(&group->entry); xorg_list_del(&group->entry);
for (list = group->values; *list; list++) for (list = group->values; *list; list++) {
free(*list); free(*list);
*list = NULL;
}
free(group); free(group);
group = NULL;
} }
xf86optionListFree(ptr->option_lst); xf86optionListFree(ptr->option_lst);

View File

@ -569,7 +569,6 @@ static char *
DoSubstitution(const char *template, const char *cmdline, const char *projroot, DoSubstitution(const char *template, const char *cmdline, const char *projroot,
int *cmdlineUsed, int *envUsed, const char *XConfigFile) int *cmdlineUsed, int *envUsed, const char *XConfigFile)
{ {
char *result;
int i, l; int i, l;
static const char *env = NULL; static const char *env = NULL;
static char *hostname = NULL; static char *hostname = NULL;
@ -582,7 +581,10 @@ DoSubstitution(const char *template, const char *cmdline, const char *projroot,
if (envUsed) if (envUsed)
*envUsed = 0; *envUsed = 0;
result = malloc(PATH_MAX + 1); char *result = calloc(1, PATH_MAX + 1);
if (!result)
return NULL;
l = 0; l = 0;
for (i = 0; template[i]; i++) { for (i = 0; template[i]; i++) {
if (template[i] != '%') { if (template[i] != '%') {