json-eval-rs 0.0.98

High-performance JSON Logic evaluator with schema validation and dependency tracking. Built on blazing-fast Rust engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! Core FFI functions: version, constructors, memory management

use super::types::{FFIResult, JSONEvalHandle};
use crate::JSONEval;
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::ptr;

/// Get the library version
///
/// Returns a pointer to a static null-terminated string containing the version.
/// This pointer does not need to be freed.
#[no_mangle]
pub extern "C" fn json_eval_version() -> *const c_char {
    concat!(env!("CARGO_PKG_VERSION"), "\0").as_ptr() as *const c_char
}

/// Create a new JSONEval instance from MessagePack-encoded schema
///
/// # Safety
///
/// - schema_msgpack must be a valid pointer to MessagePack-encoded bytes
/// - schema_len must be the exact length of the MessagePack data
/// - context can be NULL for no context
/// - data can be NULL for no initial data
/// - Caller must call json_eval_free when done
#[no_mangle]
pub unsafe extern "C" fn json_eval_new_from_msgpack(
    schema_msgpack: *const u8,
    schema_len: usize,
    context: *const c_char,
    data: *const c_char,
) -> *mut JSONEvalHandle {
    if schema_msgpack.is_null() || schema_len == 0 {
        eprintln!("[FFI ERROR] json_eval_new_from_msgpack: invalid schema pointer or length");
        return ptr::null_mut();
    }

    // Convert raw pointer to slice
    let schema_bytes = std::slice::from_raw_parts(schema_msgpack, schema_len);

    let context_str = if !context.is_null() {
        match CStr::from_ptr(context).to_str() {
            Ok(s) => Some(s),
            Err(e) => {
                eprintln!(
                    "[FFI ERROR] json_eval_new_from_msgpack: invalid UTF-8 in context: {}",
                    e
                );
                return ptr::null_mut();
            }
        }
    } else {
        None
    };

    let data_str = if !data.is_null() {
        match CStr::from_ptr(data).to_str() {
            Ok(s) => Some(s),
            Err(e) => {
                eprintln!(
                    "[FFI ERROR] json_eval_new_from_msgpack: invalid UTF-8 in data: {}",
                    e
                );
                return ptr::null_mut();
            }
        }
    } else {
        None
    };

    match JSONEval::new_from_msgpack(schema_bytes, context_str, data_str) {
        Ok(eval) => {
            let handle = Box::new(JSONEvalHandle {
                inner: Box::new(eval),
                current_token: None,
            });
            Box::into_raw(handle)
        }
        Err(e) => {
            let error_msg = format!("Failed to create JSONEval instance from MessagePack: {}", e);
            eprintln!("[FFI ERROR] json_eval_new_from_msgpack: {}", error_msg);
            ptr::null_mut()
        }
    }
}

/// Create a new JSONEval instance
///
/// # Safety
///
/// - schema must be a valid null-terminated UTF-8 string
/// - context can be NULL for no context
/// - data can be NULL for no initial data
/// - Caller must call json_eval_free when done
#[no_mangle]
pub unsafe extern "C" fn json_eval_new(
    schema: *const c_char,
    context: *const c_char,
    data: *const c_char,
) -> *mut JSONEvalHandle {
    if schema.is_null() {
        eprintln!("[FFI ERROR] json_eval_new: schema pointer is null");
        return ptr::null_mut();
    }

    let schema_str = match CStr::from_ptr(schema).to_str() {
        Ok(s) => s,
        Err(e) => {
            eprintln!("[FFI ERROR] json_eval_new: invalid UTF-8 in schema: {}", e);
            return ptr::null_mut();
        }
    };

    let context_str = if !context.is_null() {
        match CStr::from_ptr(context).to_str() {
            Ok(s) => Some(s),
            Err(e) => {
                eprintln!("[FFI ERROR] json_eval_new: invalid UTF-8 in context: {}", e);
                return ptr::null_mut();
            }
        }
    } else {
        None
    };

    let data_str = if !data.is_null() {
        match CStr::from_ptr(data).to_str() {
            Ok(s) => Some(s),
            Err(e) => {
                eprintln!("[FFI ERROR] json_eval_new: invalid UTF-8 in data: {}", e);
                return ptr::null_mut();
            }
        }
    } else {
        None
    };

    match JSONEval::new(schema_str, context_str, data_str) {
        Ok(eval) => {
            let handle = Box::new(JSONEvalHandle {
                inner: Box::new(eval),
                current_token: None,
            });
            Box::into_raw(handle)
        }
        Err(e) => {
            let error_msg = format!("Failed to create JSONEval instance: {}", e);
            eprintln!("[FFI ERROR] json_eval_new: {}", error_msg);
            ptr::null_mut()
        }
    }
}

/// Create a new JSONEval instance with detailed error reporting
///
/// # Safety
///
/// - schema must be a valid null-terminated UTF-8 string
/// - context can be NULL for no context
/// - data can be NULL for no initial data
/// - error_out must be a valid pointer to store error message (caller owns the string)
/// - Returns non-null handle on success, null on failure (check error_out for details)
#[no_mangle]
pub unsafe extern "C" fn json_eval_new_with_error(
    schema: *const c_char,
    context: *const c_char,
    data: *const c_char,
    error_out: *mut *mut c_char,
) -> *mut JSONEvalHandle {
    if schema.is_null() {
        if !error_out.is_null() {
            *error_out = CString::new("Schema pointer is null")
                .unwrap_or_else(|_| CString::new("Null byte error").unwrap())
                .into_raw();
        }
        return ptr::null_mut();
    }

    let schema_str = match CStr::from_ptr(schema).to_str() {
        Ok(s) => s,
        Err(e) => {
            if !error_out.is_null() {
                let msg = format!("Invalid UTF-8 in schema: {}", e);
                *error_out = CString::new(msg)
                    .unwrap_or_else(|_| CString::new("Invalid UTF-8 in schema").unwrap())
                    .into_raw();
            }
            return ptr::null_mut();
        }
    };

    let context_str = if !context.is_null() {
        match CStr::from_ptr(context).to_str() {
            Ok(s) => Some(s),
            Err(e) => {
                if !error_out.is_null() {
                    let msg = format!("Invalid UTF-8 in context: {}", e);
                    *error_out = CString::new(msg)
                        .unwrap_or_else(|_| CString::new("Invalid UTF-8 in context").unwrap())
                        .into_raw();
                }
                return ptr::null_mut();
            }
        }
    } else {
        None
    };

    let data_str = if !data.is_null() {
        match CStr::from_ptr(data).to_str() {
            Ok(s) => Some(s),
            Err(e) => {
                if !error_out.is_null() {
                    let msg = format!("Invalid UTF-8 in data: {}", e);
                    *error_out = CString::new(msg)
                        .unwrap_or_else(|_| CString::new("Invalid UTF-8 in data").unwrap())
                        .into_raw();
                }
                return ptr::null_mut();
            }
        }
    } else {
        None
    };

    match JSONEval::new(schema_str, context_str, data_str) {
        Ok(eval) => {
            let handle = Box::new(JSONEvalHandle {
                inner: Box::new(eval),
                current_token: None,
            });
            Box::into_raw(handle)
        }
        Err(e) => {
            if !error_out.is_null() {
                let msg = format!("Failed to create JSONEval instance: {}", e);
                *error_out = CString::new(msg)
                    .unwrap_or_else(|_| CString::new("Failed to create JSONEval instance").unwrap())
                    .into_raw();
            }
            ptr::null_mut()
        }
    }
}

/// Free an FFIResult
///
/// # Safety
///
/// - result must be a valid FFIResult from one of the evaluate functions
/// - result should not be used after calling this function
#[no_mangle]
pub unsafe extern "C" fn json_eval_free_result(result: super::types::FFIResult) {
    if !result._owned_data.is_null() {
        drop(Box::from_raw(result._owned_data));
    }
    if !result.error.is_null() {
        drop(CString::from_raw(result.error));
    }
}

/// Free a string returned by the library
///
/// # Safety
///
/// - ptr must be a valid pointer from a library function
#[no_mangle]
pub unsafe extern "C" fn json_eval_free_string(ptr: *mut c_char) {
    if !ptr.is_null() {
        drop(CString::from_raw(ptr));
    }
}

/// Free a JSONEval instance
///
/// # Safety
///
/// - handle must be a valid pointer from json_eval_new
/// - handle should not be used after calling this function
#[no_mangle]
pub unsafe extern "C" fn json_eval_free(handle: *mut JSONEvalHandle) {
    if !handle.is_null() {
        drop(Box::from_raw(handle));
    }
}

/// Reload schema with new data
///
/// # Safety
///
/// - handle must be a valid pointer from json_eval_new
/// - schema must be a valid null-terminated UTF-8 string
/// - context and data can be NULL
#[no_mangle]
pub unsafe extern "C" fn json_eval_reload_schema(
    handle: *mut JSONEvalHandle,
    schema: *const c_char,
    context: *const c_char,
    data: *const c_char,
) -> FFIResult {
    if handle.is_null() || schema.is_null() {
        return FFIResult::error("Invalid handle or schema pointer".to_string());
    }

    let eval = &mut (*handle).inner;

    let schema_str = match CStr::from_ptr(schema).to_str() {
        Ok(s) => s,
        Err(_) => return FFIResult::error("Invalid UTF-8 in schema".to_string()),
    };

    let context_str = if !context.is_null() {
        match CStr::from_ptr(context).to_str() {
            Ok(s) => Some(s),
            Err(_) => return FFIResult::error("Invalid UTF-8 in context".to_string()),
        }
    } else {
        None
    };

    let data_str = if !data.is_null() {
        match CStr::from_ptr(data).to_str() {
            Ok(s) => Some(s),
            Err(_) => return FFIResult::error("Invalid UTF-8 in data".to_string()),
        }
    } else {
        None
    };

    match eval.reload_schema(schema_str, context_str, data_str) {
        Ok(_) => FFIResult::success(Vec::new()),
        Err(e) => FFIResult::error(e),
    }
}

/// Reload schema from MessagePack-encoded bytes
///
/// # Safety
///
/// - handle must be a valid pointer from json_eval_new
/// - schema_msgpack must be a valid pointer to MessagePack bytes
/// - schema_len must be the exact length of the MessagePack data
/// - context and data can be NULL
#[no_mangle]
pub unsafe extern "C" fn json_eval_reload_schema_msgpack(
    handle: *mut JSONEvalHandle,
    schema_msgpack: *const u8,
    schema_len: usize,
    context: *const c_char,
    data: *const c_char,
) -> FFIResult {
    if handle.is_null() || schema_msgpack.is_null() || schema_len == 0 {
        return FFIResult::error("Invalid handle, schema pointer, or length".to_string());
    }

    let eval = &mut (*handle).inner;

    let schema_bytes = std::slice::from_raw_parts(schema_msgpack, schema_len);

    let context_str = if !context.is_null() {
        match CStr::from_ptr(context).to_str() {
            Ok(s) => Some(s),
            Err(_) => return FFIResult::error("Invalid UTF-8 in context".to_string()),
        }
    } else {
        None
    };

    let data_str = if !data.is_null() {
        match CStr::from_ptr(data).to_str() {
            Ok(s) => Some(s),
            Err(_) => return FFIResult::error("Invalid UTF-8 in data".to_string()),
        }
    } else {
        None
    };

    match eval.reload_schema_msgpack(schema_bytes, context_str, data_str) {
        Ok(_) => FFIResult::success(Vec::new()),
        Err(e) => FFIResult::error(e),
    }
}

/// Create a new JSONEval instance from a cached ParsedSchema
///
/// # Safety
///
/// - cache_key must be a valid null-terminated UTF-8 string
/// - context and data can be NULL
/// - Returns non-null handle on success, null on failure
/// - Caller must call json_eval_free when done
#[no_mangle]
pub unsafe extern "C" fn json_eval_new_from_cache(
    cache_key: *const c_char,
    context: *const c_char,
    data: *const c_char,
) -> *mut JSONEvalHandle {
    if cache_key.is_null() {
        eprintln!("[FFI ERROR] json_eval_new_from_cache: cache_key pointer is null");
        return ptr::null_mut();
    }

    let key_str = match CStr::from_ptr(cache_key).to_str() {
        Ok(s) => s,
        Err(e) => {
            eprintln!(
                "[FFI ERROR] json_eval_new_from_cache: invalid UTF-8 in cache_key: {}",
                e
            );
            return ptr::null_mut();
        }
    };

    let context_str = if !context.is_null() {
        match CStr::from_ptr(context).to_str() {
            Ok(s) => Some(s),
            Err(e) => {
                eprintln!(
                    "[FFI ERROR] json_eval_new_from_cache: invalid UTF-8 in context: {}",
                    e
                );
                return ptr::null_mut();
            }
        }
    } else {
        None
    };

    let data_str = if !data.is_null() {
        match CStr::from_ptr(data).to_str() {
            Ok(s) => Some(s),
            Err(e) => {
                eprintln!(
                    "[FFI ERROR] json_eval_new_from_cache: invalid UTF-8 in data: {}",
                    e
                );
                return ptr::null_mut();
            }
        }
    } else {
        None
    };

    // Get the cached ParsedSchema
    let parsed = match crate::PARSED_SCHEMA_CACHE.get(key_str) {
        Some(p) => p,
        None => {
            eprintln!(
                "[FFI ERROR] json_eval_new_from_cache: schema '{}' not found in cache",
                key_str
            );
            return ptr::null_mut();
        }
    };

    // Create JSONEval from the cached ParsedSchema
    match crate::JSONEval::with_parsed_schema(parsed, context_str, data_str) {
        Ok(eval) => {
            let handle = Box::new(JSONEvalHandle {
                inner: Box::new(eval),
                current_token: None,
            });
            Box::into_raw(handle)
        }
        Err(e) => {
            let error_msg = format!("Failed to create JSONEval instance from cache: {}", e);
            eprintln!("[FFI ERROR] json_eval_new_from_cache: {}", error_msg);
            ptr::null_mut()
        }
    }
}

/// Create a new JSONEval instance from cache with detailed error reporting
///
/// # Safety
///
/// - cache_key must be a valid null-terminated UTF-8 string
/// - context and data can be NULL
/// - error_out must be a valid pointer to store error message (caller owns the string)
/// - Returns non-null handle on success, null on failure (check error_out for details)
#[no_mangle]
pub unsafe extern "C" fn json_eval_new_from_cache_with_error(
    cache_key: *const c_char,
    context: *const c_char,
    data: *const c_char,
    error_out: *mut *mut c_char,
) -> *mut JSONEvalHandle {
    if cache_key.is_null() {
        if !error_out.is_null() {
            *error_out = CString::new("cache_key pointer is null")
                .unwrap_or_else(|_| CString::new("Null byte error").unwrap())
                .into_raw();
        }
        return ptr::null_mut();
    }

    let key_str = match CStr::from_ptr(cache_key).to_str() {
        Ok(s) => s,
        Err(e) => {
            if !error_out.is_null() {
                let error_msg = format!("Invalid UTF-8 in cache_key: {}", e);
                *error_out = CString::new(error_msg)
                    .unwrap_or_else(|_| CString::new("Invalid UTF-8 in cache_key").unwrap())
                    .into_raw();
            }
            return ptr::null_mut();
        }
    };

    let context_str = if !context.is_null() {
        match CStr::from_ptr(context).to_str() {
            Ok(s) => Some(s),
            Err(e) => {
                if !error_out.is_null() {
                    let error_msg = format!("Invalid UTF-8 in context: {}", e);
                    *error_out = CString::new(error_msg)
                        .unwrap_or_else(|_| CString::new("Invalid UTF-8 in context").unwrap())
                        .into_raw();
                }
                return ptr::null_mut();
            }
        }
    } else {
        None
    };

    let data_str = if !data.is_null() {
        match CStr::from_ptr(data).to_str() {
            Ok(s) => Some(s),
            Err(e) => {
                if !error_out.is_null() {
                    let error_msg = format!("Invalid UTF-8 in data: {}", e);
                    *error_out = CString::new(error_msg)
                        .unwrap_or_else(|_| CString::new("Invalid UTF-8 in data").unwrap())
                        .into_raw();
                }
                return ptr::null_mut();
            }
        }
    } else {
        None
    };

    // Get the cached ParsedSchema
    let parsed = match crate::PARSED_SCHEMA_CACHE.get(key_str) {
        Some(p) => p,
        None => {
            if !error_out.is_null() {
                let error_msg = format!("Schema '{}' not found in cache", key_str);
                *error_out = CString::new(error_msg)
                    .unwrap_or_else(|_| CString::new("Schema not found in cache").unwrap())
                    .into_raw();
            }
            return ptr::null_mut();
        }
    };

    // Create JSONEval from the cached ParsedSchema
    match crate::JSONEval::with_parsed_schema(parsed, context_str, data_str) {
        Ok(eval) => {
            if !error_out.is_null() {
                *error_out = ptr::null_mut(); // No error
            }
            let handle = Box::new(JSONEvalHandle {
                inner: Box::new(eval),
                current_token: None,
            });
            Box::into_raw(handle)
        }
        Err(e) => {
            if !error_out.is_null() {
                let error_msg = format!("Failed to create JSONEval from cache: {}", e);
                *error_out = CString::new(error_msg)
                    .unwrap_or_else(|_| {
                        CString::new("Failed to create JSONEval from cache").unwrap()
                    })
                    .into_raw();
            }
            ptr::null_mut()
        }
    }
}

/// Reload schema from ParsedSchemaCache using a cache key
///
/// # Safety
///
/// - handle must be a valid pointer from json_eval_new
/// - cache_key must be a valid UTF-8 string
/// - context and data can be NULL
#[no_mangle]
pub unsafe extern "C" fn json_eval_reload_schema_from_cache(
    handle: *mut JSONEvalHandle,
    cache_key: *const c_char,
    context: *const c_char,
    data: *const c_char,
) -> FFIResult {
    if handle.is_null() || cache_key.is_null() {
        return FFIResult::error("Invalid handle or cache_key".to_string());
    }

    let eval = &mut (*handle).inner;

    let key_str = match CStr::from_ptr(cache_key).to_str() {
        Ok(s) => s,
        Err(_) => return FFIResult::error("Invalid UTF-8 in cache_key".to_string()),
    };

    let context_str = if !context.is_null() {
        match CStr::from_ptr(context).to_str() {
            Ok(s) => Some(s),
            Err(_) => return FFIResult::error("Invalid UTF-8 in context".to_string()),
        }
    } else {
        None
    };

    let data_str = if !data.is_null() {
        match CStr::from_ptr(data).to_str() {
            Ok(s) => Some(s),
            Err(_) => return FFIResult::error("Invalid UTF-8 in data".to_string()),
        }
    } else {
        None
    };

    match eval.reload_schema_from_cache(key_str, context_str, data_str) {
        Ok(_) => FFIResult::success(Vec::new()),
        Err(e) => FFIResult::error(e),
    }
}

/// Set timezone offset for datetime operations (TODAY, NOW)
///
/// # Safety
///
/// - handle must be a valid pointer from json_eval_new
/// - Pass offset_minutes as the timezone offset in minutes from UTC
///   (e.g., 420 for UTC+7, -300 for UTC-5)
/// - Pass i32::MIN to reset to UTC (no offset)
///
/// # Example
///
/// ```c
/// // Set to UTC+7 (Jakarta, Bangkok)
/// json_eval_set_timezone_offset(handle, 420);
///
/// // Set to UTC-5 (New York, EST)
/// json_eval_set_timezone_offset(handle, -300);
///
/// // Reset to UTC
/// json_eval_set_timezone_offset(handle, i32::MIN);
/// ```
#[no_mangle]
pub unsafe extern "C" fn json_eval_set_timezone_offset(
    handle: *mut JSONEvalHandle,
    offset_minutes: i32,
) {
    if handle.is_null() {
        eprintln!("[FFI ERROR] json_eval_set_timezone_offset: handle is null");
        return;
    }

    let eval = &mut (*handle).inner;

    // Use i32::MIN as sentinel value for None/reset to UTC
    let offset = if offset_minutes == i32::MIN {
        None
    } else {
        Some(offset_minutes)
    };

    eval.set_timezone_offset(offset);
}

/// Cancel any currently running operation
///
/// # Safety
///
/// - handle must be a valid pointer from json_eval_new
#[no_mangle]
pub unsafe extern "C" fn json_eval_cancel(handle: *mut JSONEvalHandle) {
    if handle.is_null() {
        return;
    }
    if let Some(token) = &(*handle).current_token {
        token.cancel();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::ffi::CString;

    #[test]
    fn test_safe_cstring_handling() {
        let mut error_out: *mut c_char = ptr::null_mut();

        // This helper simulates the pattern used in the FFI functions
        unsafe fn set_error(error_out: *mut *mut c_char, msg: String) {
            if !error_out.is_null() {
                *error_out = CString::new(msg)
                    .unwrap_or_else(|_| CString::new("Fallback error message").unwrap())
                    .into_raw();
            }
        }

        // Test with safe string
        unsafe {
            set_error(&mut error_out, "Normal error".to_string());
            assert!(!error_out.is_null());
            let s = CString::from_raw(error_out);
            assert_eq!(s.to_str().unwrap(), "Normal error");
        }

        // Test with string containing null byte (the vulnerability)
        unsafe {
            set_error(&mut error_out, "Error with\0 null byte".to_string());
            assert!(!error_out.is_null());
            let s = CString::from_raw(error_out);
            assert_eq!(s.to_str().unwrap(), "Fallback error message");
        }
    }
}