From 5ccfc939d6d28b0a1980fc57a3303a8b4dbc6c85 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sat, 1 Nov 2014 16:42:07 +0300 Subject: [PATCH 1/2] Fix undefined behavior This code crashes with clang 3.4.1+, most likely due to arithmetics to null pointer, which is undefined behavior --- src/fileutil.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/fileutil.c b/src/fileutil.c index d902a6a..b77d210 100644 --- a/src/fileutil.c +++ b/src/fileutil.c @@ -71,13 +71,12 @@ char *find_file(char *name, const char *path) char *q; char path_buf[PATH_BUF_SIZE]; char dir_sep[2]={DIR_SEP,0}; - for (p=path;p;p=q+1) { + for (p=path;p;) { q=strchr(p,LIST_SEP); if (q) { if (!prepare_path_buf(path_buf,p,q)) continue; } else { - q--; if (!prepare_path_buf(path_buf,p,p+strlen(p))) continue; } strcat(path_buf,dir_sep); /* always one char */ @@ -88,6 +87,11 @@ char *find_file(char *name, const char *path) free(name); return strdup(path_buf); } + if (q) { + p = q + 1; + } else { + p = NULL; + } } /* if we are here, nothing found */ free(name); From 02fef1bfd0c67f3765f0e43fba1b1386ced14d6d Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Sat, 1 Nov 2014 16:46:43 +0300 Subject: [PATCH 2/2] Fix another UB case --- src/fileutil.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/fileutil.c b/src/fileutil.c index b77d210..6afddec 100644 --- a/src/fileutil.c +++ b/src/fileutil.c @@ -190,7 +190,7 @@ void list_charsets(void) { int count,glob_flags=GLOB_ERR; #endif char **ptr; - for (p=charset_path;p;p=q+1) { + for (p=charset_path;p;) { q=strchr(p,LIST_SEP); if (q) { @@ -201,7 +201,6 @@ void list_charsets(void) { strncpy(path_buf,p,q-p); path_buf[q-p]=0; } else { - q--; if (strlen(p)>=PATH_BUF_SIZE) continue; strcpy(path_buf,p); } @@ -248,6 +247,11 @@ void list_charsets(void) { } glob_flags|=GLOB_APPEND; #endif + if (q) { + p = q + 1; + } else { + p = NULL; + } } #ifdef __MSDOS__ fputs("utf-8\n",stdout);