From 9ca7f5ce8634b3fd97570e8edac9f5e94ee262fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20E=C3=9Fer?= Date: Mon, 2 Aug 1999 17:50:07 +0000 Subject: [PATCH] Remove accidental 8bit character in error message. Add checks for malloc failure and add error exit in case of malloc failure. Add rcsid. Submitted by: charnier --- usr.sbin/elf2exe/elf2exe.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/usr.sbin/elf2exe/elf2exe.c b/usr.sbin/elf2exe/elf2exe.c index 92919ed61d96..c7782992807d 100644 --- a/usr.sbin/elf2exe/elf2exe.c +++ b/usr.sbin/elf2exe/elf2exe.c @@ -22,16 +22,19 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * - * $Id$ */ /* * Make an ARC firmware executable from an ELF file. */ -#include +#ifndef lint +static const char rcsid[] = + "$Id$"; +#endif /* not lint */ +#include +#include #include #include #include @@ -154,6 +157,8 @@ find_shstrtable(int fileno, int sections, Elf64_Shdr *shdr) shstrtabindex = i; shstrtab = malloc(shdr[shstrtabindex].sh_size); + if (shstrtab == NULL) + errx(1, "malloc failed"); lseek(fileno, shdr[shstrtabindex].sh_offset, SEEK_SET); read(fileno, shstrtab, shdr[shstrtabindex].sh_size); @@ -243,6 +248,13 @@ section_fpos(Elf64_Shdr *shdr, int sections, char *name) return shdr[i].sh_offset; } +void +usage(void) +{ + fprintf(stderr, "usage: elf2exe infile outfile\n"); + exit(1); +} + int main(int argc, char** argv) { @@ -262,13 +274,13 @@ main(int argc, char** argv) int sections; if (argc != 3) - errx(1, "usage: elf2exe "); + usage(); infd = open_elffile(argv[1]); ehdr = load_ehdr(infd); if (ehdr == NULL) - errx(1, "canīt read Elf Header\n"); + errx(1, "cannot read Elf Header\n"); sections = ehdr->e_shnum; progentry = ehdr->e_entry; @@ -361,6 +373,8 @@ main(int argc, char** argv) lseek(outfd, textscn.s_scnptr, SEEK_SET); p = malloc(ROUNDUP(textsize, 512)); + if (p == NULL) + errx(1, "malloc failed"); memset(p, 0, ROUNDUP(textsize, 512)); lseek(infd, textfpos, SEEK_SET); read(infd, p, textsize); @@ -369,6 +383,8 @@ main(int argc, char** argv) lseek(outfd, datascn.s_scnptr, SEEK_SET); p = malloc(ROUNDUP(datasize, 512)); + if (p == NULL) + errx(1, "malloc failed"); memset(p, 0, ROUNDUP(datasize, 512)); lseek(infd, datafpos, SEEK_SET); read(infd, p, datasize);