From 9dccedb6d3a10f2ae23a19d9368ed96136f58038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Sat, 22 Oct 2005 13:15:43 +0000 Subject: [PATCH] =?UTF-8?q?Check=20return=20value=20from=20asprintf=20inst?= =?UTF-8?q?ead=20of=20string=20!=3D=20NULL=20since=20it=20undefined=20beha?= =?UTF-8?q?vior=20on=20Linux.=20From=20Bj=C3=B6rn=20Sandell?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@16216 ec53bebd-3082-4978-b11e-865c3cabbd6b --- appl/ftp/ftpd/ls.c | 79 ++++++++++++++++++++++++++++---------- appl/login/login.c | 3 +- appl/popper/pop_dropinfo.c | 3 +- 3 files changed, 61 insertions(+), 24 deletions(-) diff --git a/appl/ftp/ftpd/ls.c b/appl/ftp/ftpd/ls.c index e34e71008..190f8c514 100644 --- a/appl/ftp/ftpd/ls.c +++ b/appl/ftp/ftpd/ls.c @@ -146,16 +146,16 @@ block_convert(size_t blocks) #endif } -static void +static int make_fileinfo(FILE *out, const char *filename, struct fileinfo *file, int flags) { char buf[128]; int file_type = 0; struct stat *st = &file->st; - + file->inode = st->st_ino; file->bsize = block_convert(st->st_blocks); - + if(S_ISDIR(st->st_mode)) { file->mode[0] = 'd'; file_type = '/'; @@ -218,31 +218,51 @@ make_fileinfo(FILE *out, const char *filename, struct fileinfo *file, int flags) { struct passwd *pwd; pwd = getpwuid(st->st_uid); - if(pwd == NULL) - asprintf(&file->user, "%u", (unsigned)st->st_uid); - else + if(pwd == NULL) { + if (asprintf(&file->user, "%u", (unsigned)st->st_uid) == -1) + file->user = NULL; + } else file->user = strdup(pwd->pw_name); + if (file->user == NULL) { + syslog(LOG_ERR, "out of memory"); + return -1; + } } { struct group *grp; grp = getgrgid(st->st_gid); - if(grp == NULL) - asprintf(&file->group, "%u", (unsigned)st->st_gid); - else + if(grp == NULL) { + if (asprintf(&file->group, "%u", (unsigned)st->st_gid) == -1) + file->group = NULL; + } else file->group = strdup(grp->gr_name); + if (file->group == NULL) { + syslog(LOG_ERR, "out of memory"); + return -1; + } } if(S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) { #if defined(major) && defined(minor) - asprintf(&file->major, "%u", (unsigned)major(st->st_rdev)); - asprintf(&file->minor, "%u", (unsigned)minor(st->st_rdev)); + if (asprintf(&file->major, "%u", (unsigned)major(st->st_rdev)) == -1) + file->major = NULL; + if (asprintf(&file->minor, "%u", (unsigned)minor(st->st_rdev)) == -1) + file->minor = NULL; #else /* Don't want to use the DDI/DKI crap. */ - asprintf(&file->major, "%u", (unsigned)st->st_rdev); - asprintf(&file->minor, "%u", 0); + if (asprintf(&file->major, "%u", (unsigned)st->st_rdev) == -1) + file->major = NULL; + if (asprintf(&file->minor, "%u", 0) == -1) + file->minor = NULL; #endif - } else - asprintf(&file->size, "%lu", (unsigned long)st->st_size); + if (file->major == NULL || file->minor == NULL) { + syslog(LOG_ERR, "out of memory"); + return -1; + } + } else { + if (asprintf(&file->size, "%lu", (unsigned long)st->st_size) == -1) + file->size = NULL; + } { time_t t = time(NULL); @@ -254,6 +274,10 @@ make_fileinfo(FILE *out, const char *filename, struct fileinfo *file, int flags) else strftime(buf, sizeof(buf), "%b %e %H:%M", tm); file->date = strdup(buf); + if (file->date == NULL) { + syslog(LOG_ERR, "out of memory"); + return -1; + } } { const char *p = strrchr(filename, '/'); @@ -261,10 +285,15 @@ make_fileinfo(FILE *out, const char *filename, struct fileinfo *file, int flags) p++; else p = filename; - if((flags & LS_TYPE) && file_type != 0) - asprintf(&file->filename, "%s%c", p, file_type); - else + if((flags & LS_TYPE) && file_type != 0) { + if (asprintf(&file->filename, "%s%c", p, file_type) == -1) + file->filename = NULL; + } else file->filename = strdup(p); + if (file->filename == NULL) { + syslog(LOG_ERR, "out of memory"); + return -1; + } } if(S_ISLNK(st->st_mode)) { int n; @@ -272,9 +301,14 @@ make_fileinfo(FILE *out, const char *filename, struct fileinfo *file, int flags) if(n >= 0) { buf[n] = '\0'; file->link = strdup(buf); + if (file->link == NULL) { + syslog(LOG_ERR, "out of memory"); + return -1; + } } else sec_fprintf2(out, "readlink(%s): %s", filename, strerror(errno)); } + return 0; } static void @@ -508,7 +542,9 @@ list_files(FILE *out, const char **files, int n_files, int flags) include_in_list = 0; } if(include_in_list) { - make_fileinfo(out, files[i], &fi[i], flags); + ret = make_fileinfo(out, files[i], &fi[i], flags); + if (ret) + goto out; n_print++; } } @@ -729,6 +765,7 @@ list_dir(FILE *out, const char *directory, int flags) struct dirent *ent; char **files = NULL; int n_files = 0; + int ret; if(d == NULL) { syslog(LOG_ERR, "%s: %m", directory); @@ -747,8 +784,8 @@ list_dir(FILE *out, const char *directory, int flags) return -1; } files = tmp; - asprintf(&files[n_files], "%s/%s", directory, ent->d_name); - if (files[n_files] == NULL) { + ret = asprintf(&files[n_files], "%s/%s", directory, ent->d_name); + if (ret == -1) { syslog(LOG_ERR, "%s: out of memory", directory); free_files (files, n_files); closedir (d); diff --git a/appl/login/login.c b/appl/login/login.c index 28934a9c7..4aac24645 100644 --- a/appl/login/login.c +++ b/appl/login/login.c @@ -121,7 +121,8 @@ exec_shell(const char *shell, int fallback) p++; else p = shell; - asprintf(&sh, "-%s", p); + if (asprintf(&sh, "-%s", p) == -1) + errx(1, "Out of memory"); execle(shell, sh, NULL, env); if(fallback){ warnx("Can't exec %s, trying %s", diff --git a/appl/popper/pop_dropinfo.c b/appl/popper/pop_dropinfo.c index 404560747..7d1a05cd7 100644 --- a/appl/popper/pop_dropinfo.c +++ b/appl/popper/pop_dropinfo.c @@ -97,8 +97,7 @@ add_missing_headers(POP *p, MsgInfoList *mp) { #if defined(UIDL) || defined(XOVER) if (mp->msg_id == NULL) { - asprintf(&mp->msg_id, "no-message-id-%d", mp->number); - if(mp->msg_id == NULL) { + if (asprintf(&mp->msg_id, "no-message-id-%d", mp->number) == -1) fclose (p->drop); p->msg_count = 0; return pop_msg (p,POP_FAILURE,