]> Repositorios git - scryer-prolog.git/commitdiff
don't crash on empty list and differentiate list head not being string like
authorSkgland <[email protected]>
Mon, 11 Aug 2025 21:20:48 +0000 (23:20 +0200)
committerBennet Bleßmann <[email protected]>
Sun, 24 Aug 2025 17:56:25 +0000 (19:56 +0200)
src/machine/system_calls.rs

index 2d8b230eec96bf608985d21dcf3c3f02f8d3c909..8ba4fd4ca09157b83f2934e1c8427c4a76fa4a73 100644 (file)
@@ -5018,15 +5018,21 @@ impl Machine {
             // the tail are the struct field values
 
             let mut iter = args.into_iter();
-            if let Some(struct_name) = machine_st.value_to_str_like(iter.next().unwrap()) {
-                Ok(Value::Struct(
-                    struct_name.as_str().to_string(),
-                    iter.map(|x| Self::map_ffi_arg(machine_st, x, stub_gen))
-                        .collect::<Result<_, _>>()?,
-                ))
+
+            if let Some(head) = iter.next() {
+                if let Some(struct_name) = machine_st.value_to_str_like(head) {
+                    Ok(Value::Struct(
+                        struct_name.as_str().to_string(),
+                        iter.map(|x| Self::map_ffi_arg(machine_st, x, stub_gen))
+                            .collect::<Result<_, _>>()?,
+                    ))
+                } else {
+                    // first element of a struct needs to be the type
+                    Err(machine_st.error_form(machine_st.ffi_error(FfiError::ValueOutOfRange, head), stub_gen()))
+                }
             } else {
                 // empty list is an invalid struct repr
-                Err(machine_st.error_form(machine_st.ffi_error(FfiError::InvalidStruct, source), stub_gen()))
+                Err(machine_st.error_form(machine_st.ffi_error(FfiError::ValueOutOfRange, source), stub_gen()))
             }
         } else {
             Err(machine_st.error_form(machine_st.ffi_error(FfiError::InvalidArgument, source), stub_gen()))