From 33469b0e28a13c759cd63a3cb9df1641b2a860c9 Mon Sep 17 00:00:00 2001 From: isaki Date: Fri, 23 Nov 2007 04:32:14 +0000 Subject: [PATCH] loadfile_*() must set errno when it returns -1. --- sys/lib/libsa/loadfile_aout.c | 44 ++++++++++++++++++++++++++++------ sys/lib/libsa/loadfile_ecoff.c | 20 ++++++++++++---- sys/lib/libsa/loadfile_elf32.c | 37 ++++++++++++++++++++++------ sys/lib/libsa/saerrno.h | 5 ++-- 4 files changed, 85 insertions(+), 21 deletions(-) diff --git a/sys/lib/libsa/loadfile_aout.c b/sys/lib/libsa/loadfile_aout.c index 64b0e4247e64..3869466e9b91 100644 --- a/sys/lib/libsa/loadfile_aout.c +++ b/sys/lib/libsa/loadfile_aout.c @@ -1,4 +1,4 @@ -/* $NetBSD: loadfile_aout.c,v 1.9 2007/06/05 08:48:50 martin Exp $ */ +/* $NetBSD: loadfile_aout.c,v 1.10 2007/11/23 04:32:14 isaki Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -105,6 +105,7 @@ loadfile_aout(fd, x, marks, flags) paddr_t offset = marks[MARK_START]; u_long magic = N_GETMAGIC(*x); int sub; + ssize_t nr; /* some ports dont use the offset */ offset = offset; @@ -144,8 +145,13 @@ loadfile_aout(fd, x, marks, flags) if (flags & LOAD_TEXT) { PROGRESS(("%ld", x->a_text)); - if (READ(fd, maxp, x->a_text - sub) != - (ssize_t)(x->a_text - sub)) { + nr = READ(fd, maxp, x->a_text - sub); + if (nr == -1) { + WARN(("read text")); + return 1; + } + if (nr != (ssize_t)(x->a_text - sub)) { + errno = ESHORT; WARN(("read text")); return 1; } @@ -180,7 +186,13 @@ loadfile_aout(fd, x, marks, flags) PROGRESS(("+%ld", x->a_data)); marks[MARK_DATA] = LOADADDR(maxp); - if (READ(fd, maxp, x->a_data) != (ssize_t)x->a_data) { + nr = READ(fd, maxp, x->a_data); + if (nr == -1) { + WARN(("read data")); + return 1; + } + if (nr != (ssize_t)x->a_data) { + errno = ESHORT; WARN(("read data")); return 1; } @@ -225,7 +237,13 @@ loadfile_aout(fd, x, marks, flags) if (flags & LOAD_SYM) { PROGRESS(("+[%ld", x->a_syms)); - if (READ(fd, maxp, x->a_syms) != (ssize_t)x->a_syms) { + nr = READ(fd, maxp, x->a_syms); + if (nr == -1) { + WARN(("read symbols")); + return 1; + } + if (nr != (ssize_t)x->a_syms) { + errno = ESHORT; WARN(("read symbols")); return 1; } @@ -238,7 +256,13 @@ loadfile_aout(fd, x, marks, flags) if (flags & (LOAD_SYM|COUNT_SYM)) maxp += x->a_syms; - if (read(fd, &cc, sizeof(cc)) != sizeof(cc)) { + nr = read(fd, &cc, sizeof(cc)); + if (nr == -1) { + WARN(("read string table")); + return 1; + } + if (nr != sizeof(cc)) { + errno = ESHORT; WARN(("read string table")); return 1; } @@ -260,7 +284,13 @@ loadfile_aout(fd, x, marks, flags) } if (flags & LOAD_SYM) { - if (READ(fd, maxp, cc) != cc) { + nr = READ(fd, maxp, cc); + if (nr == -1) { + WARN(("read strings")); + return 1; + } + if (nr != cc) { + errno = ESHORT; WARN(("read strings")); return 1; } diff --git a/sys/lib/libsa/loadfile_ecoff.c b/sys/lib/libsa/loadfile_ecoff.c index 6fb6c5b3bfae..c9e63699ac3a 100644 --- a/sys/lib/libsa/loadfile_ecoff.c +++ b/sys/lib/libsa/loadfile_ecoff.c @@ -1,4 +1,4 @@ -/* $NetBSD: loadfile_ecoff.c,v 1.8 2007/06/06 07:56:39 martin Exp $ */ +/* $NetBSD: loadfile_ecoff.c,v 1.9 2007/11/23 04:32:14 isaki Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -66,6 +66,7 @@ loadfile_coff(fd, coff, marks, flags) { paddr_t offset = marks[MARK_START]; paddr_t minp = ~0, maxp = 0, pos; + ssize_t nr; /* some ports dont use the offset */ offset = offset; @@ -79,8 +80,12 @@ loadfile_coff(fd, coff, marks, flags) if (coff->a.tsize != 0) { if (flags & LOAD_TEXT) { PROGRESS(("%lu", coff->a.tsize)); - if (READ(fd, coff->a.text_start, coff->a.tsize) != - coff->a.tsize) { + nr = READ(fd, coff->a.text_start, coff->a.tsize); + if (nr == -1) { + return 1; + } + if (nr != coff->a.tsize) { + errno = ESHORT; return 1; } } @@ -104,8 +109,13 @@ loadfile_coff(fd, coff, marks, flags) if (coff->a.dsize != 0) { if (flags & LOAD_DATA) { PROGRESS(("+%lu", coff->a.dsize)); - if (READ(fd, coff->a.data_start, coff->a.dsize) != - coff->a.dsize) { + nr = READ(fd, coff->a.data_start, coff->a.dsize); + if (nr == -1) { + WARN(("read data")); + return 1; + } + if (nr != coff->a.dsize) { + errno = ESHORT; WARN(("read data")); return 1; } diff --git a/sys/lib/libsa/loadfile_elf32.c b/sys/lib/libsa/loadfile_elf32.c index 6ced6b9ece07..787934404370 100644 --- a/sys/lib/libsa/loadfile_elf32.c +++ b/sys/lib/libsa/loadfile_elf32.c @@ -1,4 +1,4 @@ -/* $NetBSD: loadfile_elf32.c,v 1.17 2007/06/05 08:48:50 martin Exp $ */ +/* $NetBSD: loadfile_elf32.c,v 1.18 2007/11/23 04:32:14 isaki Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -276,6 +276,7 @@ ELFNAMEEND(loadfile)(fd, elf, marks, flags) int first; paddr_t minp = ~0, maxp = 0, pos = 0; paddr_t offset = marks[MARK_START], shpp, elfp = 0; + ssize_t nr; /* some ports dont use the offset */ offset = offset; @@ -289,7 +290,13 @@ ELFNAMEEND(loadfile)(fd, elf, marks, flags) WARN(("lseek phdr")); goto freephdr; } - if (read(fd, phdr, sz) != sz) { + nr = read(fd, phdr, sz); + if (nr == -1) { + WARN(("read program headers")); + goto freephdr; + } + if (nr != sz) { + errno = ESHORT; WARN(("read program headers")); goto freephdr; } @@ -329,8 +336,13 @@ ELFNAMEEND(loadfile)(fd, elf, marks, flags) WARN(("lseek text")); goto freephdr; } - if (READ(fd, phdr[i].p_vaddr, phdr[i].p_filesz) != - (ssize_t)phdr[i].p_filesz) { + nr = READ(fd, phdr[i].p_vaddr, phdr[i].p_filesz); + if (nr == -1) { + WARN(("read text error")); + goto freephdr; + } + if (nr != (ssize_t)phdr[i].p_filesz) { + errno = ESHORT; WARN(("read text")); goto freephdr; } @@ -380,7 +392,13 @@ ELFNAMEEND(loadfile)(fd, elf, marks, flags) shp = ALLOC(sz); - if (read(fd, shp, sz) != sz) { + nr = read(fd, shp, sz); + if (nr == -1) { + WARN(("read section headers")); + goto freeshp; + } + if (nr != sz) { + errno = ESHORT; WARN(("read section headers")); goto freeshp; } @@ -422,8 +440,13 @@ ELFNAMEEND(loadfile)(fd, elf, marks, flags) WARN(("lseek symbols")); goto freeshp; } - if (READ(fd, maxp, shp[i].sh_size) != - (ssize_t)shp[i].sh_size) { + nr = READ(fd, maxp, shp[i].sh_size); + if (nr == -1) { + WARN(("read symbols")); + goto freeshp; + } + if (nr != (ssize_t)shp[i].sh_size) { + errno = ESHORT; WARN(("read symbols")); goto freeshp; } diff --git a/sys/lib/libsa/saerrno.h b/sys/lib/libsa/saerrno.h index a96bf97dfc42..ae3f4c6f3ca0 100644 --- a/sys/lib/libsa/saerrno.h +++ b/sys/lib/libsa/saerrno.h @@ -1,4 +1,4 @@ -/* $NetBSD: saerrno.h,v 1.9 2005/12/11 12:24:46 christos Exp $ */ +/* $NetBSD: saerrno.h,v 1.10 2007/11/23 04:32:14 isaki Exp $ */ /* * Copyright (c) 1988, 1993 @@ -48,4 +48,5 @@ extern int errno; #define EWCK (ELAST+10) /* write check error */ #define EECC (ELAST+11) /* uncorrectable ecc error */ #define EHER (ELAST+12) /* hard error */ -#define ESALAST (ELAST+12) /* */ +#define ESHORT (ELAST+13) /* short read */ +#define ESALAST (ELAST+13) /* */