From 5960278b8958aa7ed5fb9730e466e632ad5a21a5 Mon Sep 17 00:00:00 2001
From: Christoph Biedl <debian.axhn@manchmal.in-ulm.de>
Date: Sun, 22 Jul 2018 12:42:09 +0200
Subject: [PATCH] ELF: Handle files without program headers gracefully

Forward-ported from https://bugzilla.redhat.com/show_bug.cgi?id=109509
---
 src/readelf.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/readelf.c b/src/readelf.c
index db0b35a5..0441da41 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -352,6 +352,11 @@ dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
 	off_t ph_off = off;
 	int ph_num = num;
 
+	if (num == 0) {
+		if (file_printf(ms, ", no program header") == -1)
+			return -1;
+		return 0;
+	}
 	if (size != xph_sizeof) {
 		if (file_printf(ms, ", corrupted program header size") == -1)
 			return -1;
@@ -1278,6 +1283,11 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
 	char name[50];
 	ssize_t namesize;
 
+	if (num == 0) {
+		if (file_printf(ms, ", no section header") == -1)
+			return -1;
+		return 0;
+	}
 	if (size != xsh_sizeof) {
 		if (file_printf(ms, ", corrupted section header size") == -1)
 			return -1;
@@ -1549,6 +1559,11 @@ dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
 	ssize_t bufsize;
 	size_t offset, align, len;
 	
+	if (num == 0) {
+		if (file_printf(ms, ", no program header") == -1)
+			return -1;
+		return 0;
+	}
 	if (size != xph_sizeof) {
 		if (file_printf(ms, ", corrupted program header size") == -1)
 			return -1;
-- 
2.18.0

