From a5409ecd361da99ec99def77f28302d4225b7419 Mon Sep 17 00:00:00 2001
From: Jason Hansel <jason@jasonhansel.com>
Date: Mon, 10 Jun 2019 10:25:59 -0400
Subject: [PATCH] Fix out-of-bounds error in strsignal

---
 src/header/string/mod.rs               |  4 +++-
 tests/Makefile                         |  1 +
 tests/expected/string/strsignal.stderr |  0
 tests/expected/string/strsignal.stdout |  1 +
 tests/string/strsignal.c               | 28 ++++++++++++++++++++++++++
 5 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 tests/expected/string/strsignal.stderr
 create mode 100644 tests/expected/string/strsignal.stdout
 create mode 100644 tests/string/strsignal.c

diff --git a/src/header/string/mod.rs b/src/header/string/mod.rs
index 1bec129c0..f19e201d8 100644
--- a/src/header/string/mod.rs
+++ b/src/header/string/mod.rs
@@ -366,7 +366,9 @@ pub unsafe extern "C" fn strrchr(s: *const c_char, c: c_int) -> *mut c_char {
 
 #[no_mangle]
 pub unsafe extern "C" fn strsignal(sig: c_int) -> *const c_char {
-    signal::_signal_strings[sig as usize].as_ptr() as *const c_char
+    signal::_signal_strings.get(sig as usize).unwrap_or(
+        &signal::_signal_strings[0] // Unknown signal message
+    ).as_ptr() as *const c_char
 }
 
 #[no_mangle]
diff --git a/tests/Makefile b/tests/Makefile
index d4e0e9a40..31421c63c 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -61,6 +61,7 @@ EXPECT_NAMES=\
 	string/strstr \
 	string/strtok \
 	string/strtok_r \
+	string/strsignal \
 	strings \
 	sys_epoll/epoll \
 	time/asctime \
diff --git a/tests/expected/string/strsignal.stderr b/tests/expected/string/strsignal.stderr
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/expected/string/strsignal.stdout b/tests/expected/string/strsignal.stdout
new file mode 100644
index 000000000..833dc6308
--- /dev/null
+++ b/tests/expected/string/strsignal.stdout
@@ -0,0 +1 @@
+# strsignal #
diff --git a/tests/string/strsignal.c b/tests/string/strsignal.c
new file mode 100644
index 000000000..1ddf08630
--- /dev/null
+++ b/tests/string/strsignal.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+
+#include "test_helpers.h"
+
+int main(void) {
+    puts("# strsignal #");
+    char *x = strsignal(SIGHUP);
+    int res;
+    if (strcmp(x, "Hangup")) {
+        printf("Incorrect strsignal (1), found: .%s.\n", x);
+        exit(EXIT_FAILURE);
+    }
+    x = strsignal(0); 
+    if (strcmp(x, "Unknown signal")) {
+        printf("Incorrect strsignal (2), found: .%s.\n", x);
+        exit(EXIT_FAILURE);
+    }
+    x = strsignal(100); 
+    if (strcmp(x, "Unknown signal")) {
+        printf("Incorrect strsignal (3), found: .%s.\n", x);
+        exit(EXIT_FAILURE);
+    }
+
+
+}
-- 
GitLab