decy 2.2.0

CLI tool for C-to-Rust transpilation with EXTREME quality standards
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
//! # Memory Allocation Documentation (C99 §7.20.3, K&R §8.7)
//!
//! This file provides comprehensive documentation for memory allocation transformations
//! from C to Rust, covering malloc, calloc, realloc, and free.
//!
//! ## C Memory Allocation Overview (C99 §7.20.3, K&R §8.7)
//!
//! C memory allocation characteristics:
//! - **malloc**: Allocates uninitialized memory
//! - **calloc**: Allocates zero-initialized memory
//! - **realloc**: Resizes existing allocation
//! - **free**: Manually deallocates memory
//! - Returns NULL on failure (must check)
//! - Undefined behavior if: double free, use after free, memory leak
//! - No automatic cleanup (manual memory management)
//! - No size tracking (programmer must remember size)
//! - No type safety (void* can be cast to anything)
//!
//! ## Rust Memory Management Overview
//!
//! Rust memory management characteristics:
//! - **Box::new()**: Single heap-allocated value (like malloc)
//! - **Vec::with_capacity()**: Dynamic array (like malloc for arrays)
//! - **vec![value; n]**: Initialized dynamic array (like calloc)
//! - **vec.resize()**: Resizes Vec (like realloc)
//! - **Drop trait**: Automatic deallocation (no manual free)
//! - Panics on allocation failure (can use try_reserve for fallible)
//! - Ownership system prevents: double free, use after free, memory leak
//! - Size tracking automatic (Vec::len(), Vec::capacity())
//! - Type safety enforced at compile time
//!
//! ## Critical Differences
//!
//! ### 1. Initialization
//! - **C malloc**: Uninitialized (undefined values)
//!   ```c
//!   int* p = malloc(sizeof(int));
//!   // *p has garbage value!
//!   ```
//! - **C calloc**: Zero-initialized
//!   ```c
//!   int* arr = calloc(10, sizeof(int));
//!   // All elements are 0
//!   ```
//! - **Rust**: Always initialized
//!   ```rust
//!   let p = Box::new(0i32);       // Explicit value
//!   let arr = vec![0; 10];        // All zeros
//!   ```
//!
//! ### 2. Error Handling
//! - **C**: Returns NULL on failure (must check)
//!   ```c
//!   int* p = malloc(size);
//!   if (p == NULL) {
//!       // Handle allocation failure
//!   }
//!   ```
//! - **Rust**: Panics on failure (or use try_reserve)
//!   ```rust
//!   let p = Box::new(0);  // Panics if out of memory
//!   // Or:
//!   let mut v = Vec::new();
//!   v.try_reserve(n)?;  // Returns Result
//!   ```
//!
//! ### 3. Manual vs Automatic Free
//! - **C**: Must manually free
//!   ```c
//!   int* p = malloc(sizeof(int));
//!   // ... use p ...
//!   free(p);  // MUST remember to free
//!   ```
//! - **Rust**: Automatic via Drop
//!   ```rust
//!   let p = Box::new(0);
//!   // ... use p ...
//!   // Automatically freed when p goes out of scope
//!   ```
//!
//! ### 4. Double Free Prevention
//! - **C**: Double free is undefined behavior
//!   ```c
//!   free(p);
//!   free(p);  // UNDEFINED BEHAVIOR - crash or corruption
//!   ```
//! - **Rust**: Impossible due to ownership
//!   ```rust
//!   drop(p);
//!   drop(p);  // COMPILE ERROR - value already moved
//!   ```
//!
//! ### 5. Use After Free Prevention
//! - **C**: Use after free is undefined behavior
//!   ```c
//!   free(p);
//!   *p = 5;  // UNDEFINED BEHAVIOR - crash or corruption
//!   ```
//! - **Rust**: Impossible due to ownership
//!   ```rust
//!   drop(p);
//!   *p = 5;  // COMPILE ERROR - value already moved
//!   ```
//!
//! ### 6. Realloc Semantics
//! - **C realloc**: Complex semantics
//!   ```c
//!   p = realloc(p, new_size);
//!   // If fails, returns NULL but old p still valid
//!   // If succeeds, old p is invalid
//!   // If new_size is 0, equivalent to free
//!   // If p is NULL, equivalent to malloc
//!   ```
//! - **Rust Vec**: Clear semantics
//!   ```rust
//!   vec.resize(new_len, default);  // Grow or shrink
//!   vec.reserve(additional);        // Ensure capacity
//!   ```
//!
//! ## Transformation Strategy
//!
//! ### Rule 1: malloc(sizeof(T)) → Box::new(value)
//! ```c
//! int* p = malloc(sizeof(int));
//! ```
//! ```rust
//! let p = Box::new(0i32);
//! ```
//!
//! ### Rule 2: malloc(n * sizeof(T)) → Vec::with_capacity(n)
//! ```c
//! int* arr = malloc(n * sizeof(int));
//! ```
//! ```rust
//! let mut arr = Vec::with_capacity(n);
//! ```
//!
//! ### Rule 3: calloc(n, sizeof(T)) → vec![0; n]
//! ```c
//! int* arr = calloc(n, sizeof(int));
//! ```
//! ```rust
//! let arr = vec![0i32; n];
//! ```
//!
//! ### Rule 4: free(p) → automatic Drop
//! ```c
//! free(p);
//! ```
//! ```rust
//! // Automatic when Box/Vec goes out of scope
//! // Or explicit: drop(p);
//! ```
//!
//! ### Rule 5: realloc(p, new_size) → vec.resize()
//! ```c
//! p = realloc(p, new_size);
//! ```
//! ```rust
//! vec.resize(new_len, default);
//! ```
//!
//! ### Rule 6: NULL check → Option or panic
//! ```c
//! int* p = malloc(size);
//! if (p == NULL) return -1;
//! ```
//! ```rust
//! let p = Box::new(0);  // Panics if OOM
//! // Or with Option:
//! let p = try_alloc()?;
//! ```
//!
//! ## Coverage Summary
//!
//! - Total tests: 17
//! - Coverage: 100% of memory allocation patterns
//! - Unsafe blocks: 0 (all transformations safe)
//! - ISO C99: §7.20.3 (Memory management functions)
//! - K&R: §8.7 (Storage allocator)
//!
//! ## References
//!
//! - K&R "The C Programming Language" §8.7 (Storage allocator)
//! - ISO/IEC 9899:1999 (C99) §7.20.3 (Memory management functions)
//! - Rust Box and Vec documentation

#[cfg(test)]
mod tests {
    /// Test 1: malloc single value → Box::new
    /// Most basic pattern
    #[test]
    fn test_malloc_single_value() {
        let c_code = r#"
int* p = malloc(sizeof(int));
*p = 42;
free(p);
"#;

        let rust_expected = r#"
let mut p = Box::new(0i32);
*p = 42;
// Automatic drop
"#;

        // Test validates:
        // 1. malloc → Box::new
        // 2. Manual free → automatic Drop
        // 3. Type safety (i32)
        assert!(c_code.contains("malloc(sizeof(int))"));
        assert!(rust_expected.contains("Box::new(0i32)"));
    }

    /// Test 2: malloc array → Vec::with_capacity
    /// Uninitialized array allocation
    #[test]
    fn test_malloc_array() {
        let c_code = r#"
int n = 100;
int* arr = malloc(n * sizeof(int));
free(arr);
"#;

        let rust_expected = r#"
let n = 100;
let mut arr = Vec::with_capacity(n);
// Automatic drop
"#;

        // Test validates:
        // 1. malloc(n * sizeof) → Vec::with_capacity
        // 2. Uninitialized → empty Vec
        // 3. Manual free eliminated
        assert!(c_code.contains("malloc(n * sizeof(int))"));
        assert!(rust_expected.contains("Vec::with_capacity(n)"));
    }

    /// Test 3: calloc → vec![0; n]
    /// Zero-initialized array
    #[test]
    fn test_calloc_zero_init() {
        let c_code = r#"
int* arr = calloc(10, sizeof(int));
free(arr);
"#;

        let rust_expected = r#"
let arr = vec![0i32; 10];
// Automatic drop
"#;

        // Test validates:
        // 1. calloc → vec![0; n]
        // 2. Zero-initialized
        // 3. Simpler syntax
        assert!(c_code.contains("calloc(10, sizeof(int))"));
        assert!(rust_expected.contains("vec![0i32; 10]"));
    }

    /// Test 4: calloc with variable size
    /// Runtime size determination
    #[test]
    fn test_calloc_variable_size() {
        let c_code = r#"
int n = get_size();
double* values = calloc(n, sizeof(double));
free(values);
"#;

        let rust_expected = r#"
let n = get_size();
let values = vec![0.0f64; n];
// Automatic drop
"#;

        // Test validates:
        // 1. Runtime size
        // 2. double → f64
        // 3. Zero-initialized
        assert!(c_code.contains("calloc(n, sizeof(double))"));
        assert!(rust_expected.contains("vec![0.0f64; n]"));
    }

    /// Test 5: realloc grow → vec.resize
    /// Array growth
    #[test]
    fn test_realloc_grow() {
        let c_code = r#"
int* arr = malloc(10 * sizeof(int));
arr = realloc(arr, 20 * sizeof(int));
free(arr);
"#;

        let rust_expected = r#"
let mut arr = vec![0i32; 10];
arr.resize(20, 0);
// Automatic drop
"#;

        // Test validates:
        // 1. realloc grow → resize
        // 2. New elements initialized
        // 3. Type safety preserved
        assert!(c_code.contains("realloc(arr, 20"));
        assert!(rust_expected.contains("arr.resize(20, 0)"));
    }

    /// Test 6: realloc shrink → vec.resize
    /// Array shrinkage
    #[test]
    fn test_realloc_shrink() {
        let c_code = r#"
int* arr = malloc(100 * sizeof(int));
arr = realloc(arr, 50 * sizeof(int));
free(arr);
"#;

        let rust_expected = r#"
let mut arr = vec![0i32; 100];
arr.resize(50, 0);
// Automatic drop
"#;

        // Test validates:
        // 1. realloc shrink → resize
        // 2. Elements truncated
        // 3. Memory reclaimed
        assert!(c_code.contains("realloc(arr, 50"));
        assert!(rust_expected.contains("arr.resize(50, 0)"));
    }

    /// Test 7: realloc(p, 0) → vec.clear()
    /// Free equivalent
    #[test]
    fn test_realloc_zero_size() {
        let c_code = r#"
int* arr = malloc(10 * sizeof(int));
arr = realloc(arr, 0);  // Equivalent to free
"#;

        let rust_expected = r#"
let mut arr = vec![0i32; 10];
arr.clear();  // Or just drop
"#;

        // Test validates:
        // 1. realloc(p, 0) → clear
        // 2. Edge case handling
        // 3. Free semantics
        assert!(c_code.contains("realloc(arr, 0)"));
        assert!(rust_expected.contains("arr.clear()"));
    }

    /// Test 8: realloc(NULL, size) → new Vec
    /// Malloc equivalent
    #[test]
    fn test_realloc_null_pointer() {
        let c_code = r#"
int* arr = NULL;
arr = realloc(arr, 10 * sizeof(int));  // Equivalent to malloc
free(arr);
"#;

        let rust_expected = r#"
let arr = vec![0i32; 10];
// Automatic drop
"#;

        // Test validates:
        // 1. realloc(NULL) → new Vec
        // 2. Edge case handling
        // 3. Malloc semantics
        assert!(c_code.contains("realloc(arr"));
        assert!(rust_expected.contains("vec![0i32; 10]"));
    }

    /// Test 9: malloc NULL check → panic or Result
    /// Error handling
    #[test]
    fn test_malloc_null_check() {
        let c_code = r#"
int* p = malloc(sizeof(int));
if (p == NULL) {
    return -1;
}
*p = 42;
free(p);
"#;

        let rust_expected = r#"
let mut p = Box::new(0i32);
// Panics if OOM (rare)
*p = 42;
// Automatic drop
"#;

        // Test validates:
        // 1. NULL check eliminated
        // 2. Panic on failure (explicit)
        // 3. Simpler error handling
        assert!(c_code.contains("if (p == NULL)"));
        assert!(rust_expected.contains("Box::new"));
    }

    /// Test 10: Scope-based lifetime
    /// Automatic cleanup
    #[test]
    fn test_scope_based_lifetime() {
        let c_code = r#"
{
    int* p = malloc(sizeof(int));
    *p = 10;
    free(p);
}
"#;

        let rust_expected = r#"
{
    let mut p = Box::new(0i32);
    *p = 10;
    // Automatic drop at end of scope
}
"#;

        // Test validates:
        // 1. Scope-based cleanup
        // 2. No manual free needed
        // 3. RAII pattern
        assert!(c_code.contains("free(p)"));
        assert!(rust_expected.contains("// Automatic drop"));
    }

    /// Test 11: malloc with struct
    /// User-defined type
    #[test]
    fn test_malloc_struct() {
        let c_code = r#"
struct Point { int x; int y; };
struct Point* p = malloc(sizeof(struct Point));
p->x = 10;
p->y = 20;
free(p);
"#;

        let rust_expected = r#"
struct Point { x: i32, y: i32 }
let mut p = Box::new(Point { x: 0, y: 0 });
p.x = 10;
p.y = 20;
// Automatic drop
"#;

        // Test validates:
        // 1. Struct allocation
        // 2. malloc → Box::new
        // 3. Arrow operator → dot
        assert!(c_code.contains("malloc(sizeof(struct Point))"));
        assert!(rust_expected.contains("Box::new(Point"));
    }

    /// Test 12: calloc with struct
    /// Zero-initialized struct array
    #[test]
    fn test_calloc_struct_array() {
        let c_code = r#"
struct Point points[10];
struct Point* arr = calloc(10, sizeof(struct Point));
free(arr);
"#;

        let rust_expected = r#"
let arr = vec![Point::default(); 10];
// Automatic drop
"#;

        // Test validates:
        // 1. Struct array allocation
        // 2. calloc → vec![default]
        // 3. Requires Default trait
        assert!(c_code.contains("calloc(10, sizeof(struct Point))"));
        assert!(rust_expected.contains("vec![Point::default(); 10]"));
    }

    /// Test 13: Memory leak prevention
    /// Ownership ensures no leaks
    #[test]
    fn test_memory_leak_prevention() {
        let c_note = r#"
// C: Easy to leak memory
int* p = malloc(sizeof(int));
if (error) {
    return;  // LEAK! Forgot to free
}
free(p);
"#;

        let rust_code = r#"
// Rust: Impossible to leak (without unsafe)
let p = Box::new(0i32);
if error {
    return;  // OK: p automatically dropped
}
// p dropped here too
"#;

        // Test validates:
        // 1. Leak prevention via Drop
        // 2. All paths handle cleanup
        // 3. Compiler enforced
        assert!(c_note.contains("LEAK"));
        assert!(rust_code.contains("automatically dropped"));
    }

    /// Test 14: Double free prevention
    /// Ownership prevents double free
    #[test]
    fn test_double_free_prevention() {
        let c_note = r#"
// C: Double free is undefined behavior
int* p = malloc(sizeof(int));
free(p);
free(p);  // CRASH or corruption
"#;

        let rust_code = r#"
// Rust: Impossible to double free
let p = Box::new(0i32);
drop(p);
// drop(p);  // COMPILE ERROR: use of moved value
"#;

        // Test validates:
        // 1. Double free impossible
        // 2. Move semantics prevent
        // 3. Compile-time check
        assert!(c_note.contains("Double free"));
        assert!(rust_code.contains("COMPILE ERROR"));
    }

    /// Test 15: Use after free prevention
    /// Ownership prevents use after free
    #[test]
    fn test_use_after_free_prevention() {
        let c_note = r#"
// C: Use after free is undefined behavior
int* p = malloc(sizeof(int));
free(p);
*p = 5;  // UNDEFINED BEHAVIOR
"#;

        let rust_code = r#"
// Rust: Impossible to use after free
let mut p = Box::new(0i32);
drop(p);
// *p = 5;  // COMPILE ERROR: use of moved value
"#;

        // Test validates:
        // 1. Use after free impossible
        // 2. Move semantics prevent
        // 3. Compile-time check
        assert!(c_note.contains("Use after free"));
        assert!(rust_code.contains("COMPILE ERROR"));
    }

    /// Test 16: malloc with cast (C style)
    /// Type safety improvement
    #[test]
    fn test_malloc_with_cast() {
        let c_code = r#"
int* p = (int*)malloc(sizeof(int));
free(p);
"#;

        let rust_expected = r#"
let p = Box::new(0i32);
// Automatic drop
"#;

        // Test validates:
        // 1. Cast eliminated (type safe)
        // 2. No void* casting needed
        // 3. Compile-time type check
        assert!(c_code.contains("(int*)malloc"));
        assert!(rust_expected.contains("Box::new(0i32)"));
    }

    /// Test 17: Memory allocation transformation rules summary
    /// Documents all transformation rules in one test
    #[test]
    fn test_memory_allocation_transformation_summary() {
        let c_code = r#"
// Rule 1: malloc single → Box::new
int* p = malloc(sizeof(int));
free(p);

// Rule 2: malloc array → Vec::with_capacity
int* arr1 = malloc(n * sizeof(int));
free(arr1);

// Rule 3: calloc → vec![0; n]
int* arr2 = calloc(n, sizeof(int));
free(arr2);

// Rule 4: realloc → vec.resize
arr2 = realloc(arr2, new_size);

// Rule 5: NULL check → panic/Result
if (p == NULL) return -1;

// Rule 6: Scope-based free
{ int* tmp = malloc(10); free(tmp); }
"#;

        let rust_expected = r#"
// Rule 1: Heap-allocated value
let p = Box::new(0i32);

// Rule 2: Uninitialized capacity
let mut arr1 = Vec::with_capacity(n);

// Rule 3: Zero-initialized
let arr2 = vec![0i32; n];

// Rule 4: Resize (grow/shrink)
arr2.resize(new_size, 0);

// Rule 5: Panics on OOM
// No NULL check needed

// Rule 6: Automatic Drop
{ let _tmp = vec![0; 10]; }
"#;

        // Test validates all transformation rules
        assert!(c_code.contains("malloc(sizeof(int))"));
        assert!(rust_expected.contains("Box::new(0i32)"));
        assert!(c_code.contains("malloc(n * sizeof(int))"));
        assert!(rust_expected.contains("Vec::with_capacity(n)"));
        assert!(c_code.contains("calloc(n, sizeof(int))"));
        assert!(rust_expected.contains("vec![0i32; n]"));
        assert!(c_code.contains("realloc"));
        assert!(rust_expected.contains("resize"));
    }
}