1From ebbc5e14cdbfcc24bf3c9bb7b41ee10cd979c535 Mon Sep 17 00:00:00 2001 2From: serge-sans-paille <serge.guelton@telecom-bretagne.eu> 3Date: Thu, 2 Feb 2023 11:40:17 +0000 4Subject: [PATCH] Fix signed vs unsigned comparison (#765) 5 6As reported by -Wsign-compare. In the case of getting the result of 7comparing the result of sysconf (_SC_PAGESIZE) to other value, this also 8correctly handles edge cases where the above fails and returns -1. 9 10Co-authored-by: serge-sans-paille <sguelton@mozilla.com> 11--- 12 src/closures.c | 2 +- 13 src/prep_cif.c | 2 +- 14 src/tramp.c | 7 +++++-- 15 3 files changed, 7 insertions(+), 4 deletions(-) 16 17diff --git a/src/closures.c b/src/closures.c 18index 9aafbec4b..c42527795 100644 19--- a/src/closures.c 20+++ b/src/closures.c 21@@ -795,7 +795,7 @@ open_temp_exec_file (void) 22 static int 23 allocate_space (int fd, off_t offset, off_t len) 24 { 25- static size_t page_size; 26+ static long page_size; 27 28 /* Obtain system page size. */ 29 if (!page_size) 30diff --git a/src/prep_cif.c b/src/prep_cif.c 31index 2d0f2521f..0e2d58e5e 100644 32--- a/src/prep_cif.c 33+++ b/src/prep_cif.c 34@@ -234,7 +234,7 @@ ffi_status ffi_prep_cif_var(ffi_cif *cif, 35 { 36 ffi_status rc; 37 size_t int_size = ffi_type_sint.size; 38- int i; 39+ unsigned int i; 40 41 rc = ffi_prep_cif_core(cif, abi, 1, nfixedargs, ntotalargs, rtype, atypes); 42 43diff --git a/src/tramp.c b/src/tramp.c 44index b9d273a1a..7e005b054 100644 45--- a/src/tramp.c 46+++ b/src/tramp.c 47@@ -266,7 +266,7 @@ ffi_tramp_get_temp_file (void) 48 * trampoline table to make sure that the temporary file can be mapped. 49 */ 50 count = write(tramp_globals.fd, tramp_globals.text, tramp_globals.map_size); 51- if (count == tramp_globals.map_size && tramp_table_alloc ()) 52+ if (count >=0 && (size_t)count == tramp_globals.map_size && tramp_table_alloc ()) 53 return 1; 54 55 close (tramp_globals.fd); 56@@ -374,6 +374,8 @@ tramp_table_unmap (struct tramp_table *table) 57 static int 58 ffi_tramp_init (void) 59 { 60+ long page_size; 61+ 62 if (tramp_globals.status == TRAMP_GLOBALS_PASSED) 63 return 1; 64 65@@ -396,7 +398,8 @@ ffi_tramp_init (void) 66 &tramp_globals.map_size); 67 tramp_globals.ntramp = tramp_globals.map_size / tramp_globals.size; 68 69- if (sysconf (_SC_PAGESIZE) > tramp_globals.map_size) 70+ page_size = sysconf (_SC_PAGESIZE); 71+ if (page_size >= 0 && (size_t)page_size > tramp_globals.map_size) 72 return 0; 73 74 if (ffi_tramp_init_os ()) 75