From 638ee993832a990aa90a4127df0668395aeeb714 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Sat, 23 Nov 2019 00:58:13 +0100
Subject: [PATCH] chore(l0der): Convert all sizes to size_t

It does not make sense for the filesize to be given as `int`; it cannot
ever be negative.  Instead, use `size_t` which is unsigned.

Signed-off-by: Rahix <rahix@rahix.de>
---
 epicardium/l0der/l0der.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/epicardium/l0der/l0der.c b/epicardium/l0der/l0der.c
index deb55571..8b0aff9b 100644
--- a/epicardium/l0der/l0der.c
+++ b/epicardium/l0der/l0der.c
@@ -149,7 +149,7 @@ static int _read_section_header(int fd, uint32_t shdr_addr, Elf32_Shdr *shdr)
  * This function ensures basic memory sanity of a program header / segment.
  * It ensures that it points to a file region that is contained within the file fully.
  */
-static int _check_program_header(int fd, int size, Elf32_Phdr *phdr)
+static int _check_program_header(int fd, size_t size, Elf32_Phdr *phdr)
 {
 	// Check file size/offset.
 	uint32_t file_start = phdr->p_offset;
@@ -191,7 +191,7 @@ static int _check_program_header(int fd, int size, Elf32_Phdr *phdr)
  * This function ensures basic memory sanity of a section header.
  * It ensures that it points to a file region that is contained within the file fully.
  */
-static int _check_section_header(int fd, int size, Elf32_Shdr *shdr)
+static int _check_section_header(int fd, size_t size, Elf32_Shdr *shdr)
 {
 	// Check file size/offset.
 	uint32_t file_start = shdr->sh_offset;
@@ -329,7 +329,7 @@ static int _load_segment(int fd, struct _pie_load_info *li, Elf32_Phdr *phdr)
  * Parse dynamic symbol sections.
  */
 static int _parse_dynamic_symbols(
-	int fd, int size, struct _pie_load_info *li, Elf32_Ehdr *hdr
+	int fd, size_t size, struct _pie_load_info *li, Elf32_Ehdr *hdr
 ) {
 	int res;
 	Elf32_Shdr shdr;
@@ -402,9 +402,9 @@ static int _parse_dynamic_symbols(
  * the only one used when making 'standard' PIE binaries on RAM. However, other
  * kinds might have to be implemented in the future.
  */
-static int
-_run_relocations(int fd, int size, struct _pie_load_info *li, Elf32_Ehdr *hdr)
-{
+static int _run_relocations(
+	int fd, size_t size, struct _pie_load_info *li, Elf32_Ehdr *hdr
+) {
 	int res;
 	Elf32_Shdr shdr;
 	Elf32_Rel rel;
@@ -513,7 +513,7 @@ _run_relocations(int fd, int size, struct _pie_load_info *li, Elf32_Ehdr *hdr)
  * Load a l0dable PIE binary.
  */
 static int
-_load_pie(int fd, int size, Elf32_Ehdr *hdr, struct l0dable_info *info)
+_load_pie(int fd, size_t size, Elf32_Ehdr *hdr, struct l0dable_info *info)
 {
 	int res;
 	struct _pie_load_info li = { 0 };
@@ -647,7 +647,10 @@ int l0der_load_path(const char *path, struct l0dable_info *info)
 		return res;
 	}
 
-	int size = epic_file_tell(fd);
+	if ((res = epic_file_tell(fd)) < 0) {
+		return res;
+	}
+	size_t size = res;
 
 	if ((res = epic_file_seek(fd, 0, SEEK_SET)) != 0) {
 		return res;
-- 
GitLab