1 example

Integer underflow

Integer falls below minimum allowable value.

[ FAQ1 ]

What is integer underflow?

Integer underflow occurs when a calculation produces a numeric value below the minimum representable limit of an integer type, causing the number to wrap around or yield incorrect results. Unlike overflow, which involves exceeding the maximum value, underflow specifically refers to falling below the minimum bound. For example, subtracting a larger number from a smaller one in an unsigned integer type can cause a wraparound to a very large value instead of a negative number. Integer underflows introduce logical errors, unexpected behaviors, and potential security vulnerabilities in applications.
[ FAQ2 ]

How to prevent integer underflow

To prevent integer underflow, always validate and constrain arithmetic operations to ensure results stay within the allowable numeric limits of the data type used. Implement explicit underflow checks prior to operations that might yield values below the minimum limit. In languages like C or C++, use safer arithmetic practices, libraries, or built-in features to automatically handle boundary conditions. Additionally, regularly employ static analyzers and runtime assertions to proactively identify and mitigate underflow scenarios, maintaining robust arithmetic integrity in your code.
diff block
+/// A progress reporter for file processing with exclusion support
+pub struct ProgressReporter {
+ pub total_files: usize,
+ pub processed: usize,
+ pub excluded_files: usize,
+ pub excluded_tags: usize,
+ pub current_file: String,
+ pub status: String,
+}
+
+impl ProgressReporter {
+ pub fn new(total_files: usize) -> Self {
+ Self {
+ total_files,
+ processed: 0,
+ excluded_files: 0,
+ excluded_tags: 0,
+ current_file: String::new(),
+ status: String::new(),
+ }
+ }
+
+ pub fn log_progress(&self) {
+ println!(
+ "\n[{}/{}] Processing: {}",
+ self.processed, self.total_files, self.current_file
+ );
+ println!("Status: {}", self.status);
+ }
+
+ pub fn log_error(&self, error: &str) {
+ eprintln!("❌ Error processing {}: {}", self.current_file, error);
+ }
+
+ pub fn log_success(&self) {
+ println!("✅ Successfully processed: {}", self.current_file);
+ }
+
+ pub fn log_warning(&self, warning: &str) {
+ println!("⚠️ Warning for {}: {}", self.current_file, warning);
+ }
+
+ pub fn log_info(&self, info: &str) {
+ println!("ℹ️ {}: {}", self.current_file, info);
+ }
+
+ pub fn log_excluded_file_impl(&mut self, path: &str, pattern: &str) {
+ self.excluded_files += 1;
+ println!("⛔ Excluding file: {} (matched pattern: {})", path, pattern);
+ }
+
+ pub fn log_excluded_tag_impl(&mut self, path: &str, tag: &str) {
+ self.excluded_tags += 1;
+ println!("⛔ Excluding file: {} (matched excluded tag: {})", path, tag);
+ }
+
+ pub fn log_summary(&self) {
+ println!("\n📊 Processing Summary");
+ println!("==================");
+ println!("✅ Successfully processed: {} files", self.processed - self.excluded_files - self.excluded_tags);
Greptile
greptile
logic: Potential integer underflow if excluded_files + excluded_tags > processed. Add bounds checking or use saturating_sub.
suggested fix
+ println!("✅ Successfully processed: {} files", self.processed.saturating_sub(self.excluded_files).saturating_sub(self.excluded_tags));