This document outlines the optimization roadmap for the Scriba codebase. The goal is to improve code quality, reduce duplication, enhance maintainability, and ensure robustness without changing functionality.
- Add error checking for all
malloc()calls - Implement consistent buffer size validation before allocation
- Add null pointer checks for all allocated memory
- Add
free()calls on all error paths - Use stack allocation or reuse buffers where possible
- Create reusable verification function for file comparison
- Consolidate progress printing logic into
timer.c - Refactor
ecc_fail_check()inspi_nand_flash.cto use a lookup table for manufacturer-specific logic - Extract common buffer allocation patterns into helper functions
- Remove duplicated verify logic in
main.c
- Create unified error handling macros
- Add validation of file operations before allocation
- Implement consistent error code propagation
- Add proper cleanup on all error paths
- Fix missing null pointer checks
- Replace byte-by-byte file comparison with
memcmp() - Optimize USB transfer buffer management in
ch341a_spi.c - Consider using
qsort()for chip detection instead of linear search - Reduce redundant file operations
- Add static analysis warnings (gcc -Wextra -Wall)
- Implement
constcorrectness for string parameters - Fix variable shadowing issues
- Add proper type definitions from
types.h - Document all external symbols in header files
File: main.c
Objective: Ensure all malloc() calls check return value
Steps:
- Allocate memory with insufficient size
- Verify program returns error code 1
- Check no segfault occurs
Expected: Error message to stderr, exit code 1
File: main.c:384-405, main.c:592-609
Objective: Verify optimized file comparison works correctly
Steps:
- Write binary file with known content
- Read file into buffer
- Compare using optimized
memcmp()instead of byte-by-byte - Verify identical files match
- Verify modified files detect mismatch
Expected: Faster comparison, correct mismatch detection
File: spi_nand_flash.c:91-361
Objective: Verify ECC check logic works with lookup table
Steps:
- Test GigaDevice chips with various ECC status register values
- Test other manufacturers (MXIC, Winbond, etc.)
- Verify bad block detection works for all chip types
- Verify no false positives
Expected: Same behavior, cleaner code structure
File: timer.c
Objective: Ensure progress display works correctly
Steps:
- Erase large chip (1MB+)
- Read large chip (1MB+)
- Write large chip (1MB+)
- Verify percentage display updates correctly
Expected: 0-100% display, no corruption
File: main.c:329-334, 422-430
Objective: Verify buffers are reused when possible
Steps:
- Test
-Wmode (erase+write+verify) - Test
-Rmode (read twice) - Verify no memory leaks
- Verify no stack corruption
Expected: Same functionality, reduced allocations
- Test 1: Buffer allocation error handling
- Add null pointer checks for all allocated memory
- Implement consistent
free()on error paths - Add validation of file sizes before allocation
- Test 2: File comparison optimization
- Create reusable verification function
- Implement
constcorrectness - Add static analysis warnings
- Test 3: ECC check table refactoring
- Consolidate progress printing
- Remove duplicated verify logic
- Extract common patterns
- Test 4: Progress printing consolidation
- Test 5: Buffer reuse optimization
- Optimize USB transfer buffer management
- Consider
qsort()for chip detection
- Run all tests with valgrind (memory leak check)
- Test with various flash chips (SPI NOR, SPI NAND, EEPROM)
- Verify backward compatibility
- Update documentation
# Build with debug symbols for testing
make CFLAGS="-DDEBUG -g"
# Memory leak check
valgrind --leak-check=full ./scriba -i
# Large chip test (1MB)
dd if=/dev/urandom of=test.bin bs=1M count=1
./scriba -W test.bin
./scriba -r test.bin
# Verify optimized comparison
./scriba -R verified.bin
# Test with various chip types
./scriba -i # Should detect flash chip- All tests pass with valgrind (no leaks)
- Code compiles with
-Wall -Wextrawithout warnings - Performance improvement: 20%+ faster file verification
- Memory usage reduced by 10%+
- Code size reduced by 15%+
- All existing functionality preserved
Each change should be:
- Tested on a known-good flash chip
- Documented in changelog
- Reverted if tests fail
- Tagged with version numbers
- Do not change functionality, only optimize implementation
- Preserve all existing command-line options
- Maintain backward compatibility with existing flash chips
- Use
gitfor version control of changes