Add some NULL checks into recently modified output functions

After a merge with master where some strings we print (such as descriptions)
could be NULL, a few segfaults popped up due to strlen() calls on null
pointers. Fix this by doing some preemptive checks and returning from
functions early if the string was null.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2008-02-24 01:17:17 -06:00
parent 105e01c8ef
commit 96f7613d15
2 changed files with 16 additions and 9 deletions

View file

@ -592,7 +592,7 @@ void cb_dl_progress(const char *filename, int file_xfered, int file_total,
/* Callback to handle notifications from the library */ /* Callback to handle notifications from the library */
void cb_log(pmloglevel_t level, char *fmt, va_list args) void cb_log(pmloglevel_t level, char *fmt, va_list args)
{ {
if(strlen(fmt) == 0) { if(!fmt || strlen(fmt) == 0) {
return; return;
} }

View file

@ -224,6 +224,10 @@ void indentprint(const char *str, int indent)
const wchar_t *p; const wchar_t *p;
int len, cidx; int len, cidx;
if(!str) {
return;
}
len = strlen(str) + 1; len = strlen(str) + 1;
wcstr = calloc(len, sizeof(wchar_t)); wcstr = calloc(len, sizeof(wchar_t));
len = mbstowcs(wcstr, str, len); len = mbstowcs(wcstr, str, len);
@ -401,14 +405,17 @@ void list_display(const char *title, const alpm_list_t *list)
int cols, len; int cols, len;
wchar_t *wcstr; wchar_t *wcstr;
/* len goes from # bytes -> # chars -> # cols */ if(title) {
len = strlen(title) + 1; /* len goes from # bytes -> # chars -> # cols */
wcstr = calloc(len, sizeof(wchar_t)); len = strlen(title) + 1;
len = mbstowcs(wcstr, title, len); wcstr = calloc(len, sizeof(wchar_t));
len = wcswidth(wcstr, len); len = mbstowcs(wcstr, title, len);
free(wcstr); len = wcswidth(wcstr, len);
free(wcstr);
printf("%s ", title); printf("%s ", title);
} else {
len = 0;
}
if(list) { if(list) {
for(i = list, cols = len; i; i = alpm_list_next(i)) { for(i = list, cols = len; i; i = alpm_list_next(i)) {