From 8dc3cca1e68768ae795ac060f7a22f24c579f651 Mon Sep 17 00:00:00 2001
From: 4lDO2 <4lDO2@protonmail.com>
Date: Sat, 10 Aug 2024 12:59:20 +0200
Subject: [PATCH] Allow (nonstandard?) pthread old{ty,state} == NULL

---
 src/header/pthread/mod.rs | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/header/pthread/mod.rs b/src/header/pthread/mod.rs
index b4bd9ab9..0b8a6ba0 100644
--- a/src/header/pthread/mod.rs
+++ b/src/header/pthread/mod.rs
@@ -1,6 +1,6 @@
 //! pthread.h implementation for Redox, following https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/pthread.h.html
 
-use core::cell::Cell;
+use core::{cell::Cell, ptr::NonNull};
 
 use crate::{
     header::{sched::*, time::timespec},
@@ -188,7 +188,11 @@ pub unsafe extern "C" fn pthread_self() -> pthread_t {
 pub unsafe extern "C" fn pthread_setcancelstate(state: c_int, oldstate: *mut c_int) -> c_int {
     match pthread::set_cancel_state(state) {
         Ok(old) => {
-            oldstate.write(old);
+            // POSIX doesn't imply oldstate can be NULL anywhere, but a lot of C code probably
+            // relies on it...
+            if let Some(oldstate) = NonNull::new(oldstate) {
+                oldstate.write(old);
+            }
             0
         }
         Err(pthread::Errno(error)) => error,
@@ -198,7 +202,11 @@ pub unsafe extern "C" fn pthread_setcancelstate(state: c_int, oldstate: *mut c_i
 pub unsafe extern "C" fn pthread_setcanceltype(ty: c_int, oldty: *mut c_int) -> c_int {
     match pthread::set_cancel_type(ty) {
         Ok(old) => {
-            oldty.write(old);
+            // POSIX doesn't imply oldty can be NULL anywhere, but a lot of C code probably relies
+            // on it...
+            if let Some(oldty) = NonNull::new(oldty) {
+                oldty.write(old);
+            }
             0
         }
         Err(pthread::Errno(error)) => error,
-- 
GitLab