Repository navigation
fix: address critical memory safety and correctness issues - #93
Merged
Merged
Conversation
Replace unsafe strcpy/strcat with bounds-checked snprintf for all filename construction operations. This prevents potential buffer overflow when user-provided filenames exceed buffer limits. Affected functions: - main(): File path construction for .fai, .mutations.txt, .mutations.vcf, and FASTQ output files
Fix loop bounds in mut_add_ins() that was accessing bases[num_ins] when valid indices are 0 to num_ins-1. This caused reading the null terminator and encoding invalid base data (4='N') as the first base of insertions. Impact: Corrupted mutation data for insertions from input files
Add NULL pointer checks after all strdup() calls in option parsing. If memory allocation fails, exit gracefully with error message instead of continuing with NULL pointer that would cause crashes. Also fixes memory leak in -q option which wasn't freeing previous value before reassignment.
Remove unused helper functions that were never called: - bases_to_iupac(): Declared but no call sites found - ran_num(): Defined but never used This reduces code maintenance burden and eliminates dead code.
Add NULL checks after all realloc() calls across the codebase. When realloc() returns NULL, the original pointer is lost causing memory leaks, and subsequent access causes crashes. Modified files: - contigs.c: contigs_add() - mut_bed.c: muts_bed_init() (2 locations) - mut_txt.c: muts_txt_init() (2 locations) - mut_vcf.c: muts_vcf_init() (4 locations) - regions_bed.c: regions_bed_init() All failures now exit with clear error messages instead of silently continuing with NULL pointers. Also adds missing stdio.h include to contigs.c for fprintf.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses multiple security vulnerabilities, memory safety issues, and functional correctness bugs discovered during code review. All changes maintain backward compatibility and pass existing tests.
Commits and Changes
1. fix: prevent buffer overflow in filename construction (991cf77)
Critical Security Fix
strcpy/strcatwith bounds-checkedsnprintfinmain()2. fix: correct off-by-one error in insertion sequence processing (480839e)
Functional Correctness Bug
mut_add_ins()(mut.c:317)bases[num_ins](null terminator) instead ofbases[num_ins-1]3. fix: add null checks after memory allocation (497607e)
Memory Safety
strdup()calls in option parsing (dwgsim_opt.c)-qoption which wasn't freeing previous value4. refactor: remove unused functions (0a64c3a)
Code Cleanup
bases_to_iupac()andran_num()- declared but never called5. fix: check realloc return values for allocation failures (bf6c10e)
Memory Safety - Comprehensive Fix
realloc()calls across 5 filescontigs.c: contigs_add()mut_bed.c: muts_bed_init() (2 locations)mut_txt.c: muts_txt_init() (2 locations)mut_vcf.c: muts_vcf_init() (4 locations)regions_bed.c: regions_bed_init()#include <stdio.h>to contigs.c forfprintfTesting
make test)Files Changed
src/dwgsim.c- Buffer overflow fix, quality score clampingsrc/dwgsim.h- Remove unused function declarationssrc/dwgsim_opt.c- NULL checks after strdupsrc/mut.c- Off-by-one fix in insertion processingsrc/contigs.c- Realloc checks + missing headersrc/mut_bed.c- Realloc checkssrc/mut_txt.c- Realloc checkssrc/mut_vcf.c- Realloc checkssrc/regions_bed.c- Realloc checksReview Notes
Each commit is focused on a specific issue type for easier review:
All changes follow defensive programming principles while maintaining the existing code style and architecture.