# AGV Path Tracking GUI - Bug Fixes Summary ## Issues Found and Fixed ### 1. **CSV Parsing Bug (path_curve_custom.cpp)** **Issue**: Incorrect error handling in CSV token parsing - **Location**: `src/path_curve_custom.cpp`, lines 35-42 (original) - **Problem**: When `std::stod()` throws an exception for a token, the code uses `continue` inside the token-reading loop. This causes the offending token to be skipped while remaining tokens are still processed, resulting in misaligned column data. - **Example**: CSV line "1.5, invalid, 3.0, 4.0" would be parsed as [1.5, 3.0, 4.0] instead of being rejected entirely. - **Fix**: - Added `parse_error` flag to track errors - When any token fails to parse, skip the entire line - Added token trimming to handle whitespace properly - Improved error handling with explicit break instead of continue ### 2. **Stanley Algorithm Index Bounds Check (control_generator.cpp)** **Issue**: Missing validation of `findNearestPoint()` return value - **Location**: `src/control_generator.cpp`, line 87 (original) - **Problem**: `findNearestPoint()` returns -1 when path is empty, but the code directly accesses `path_points[-1]` without checking, causing a crash/undefined behavior - **Crash Trace**: ```cpp int nearest_idx = path.findNearestPoint(...); PathPoint nearest_point = path_points[nearest_idx]; // CRASH if nearest_idx == -1 ``` - **Fix**: Added validation to check if `nearest_idx < 0` and default to index 0 ### 3. **Pure Pursuit Lookahead Point Type Conversion Bug (control_generator.cpp)** **Issue**: Implicit unsafe conversion of signed to unsigned integer - **Location**: `src/control_generator.cpp`, line 188 (original) - **Problem**: Converting `int nearest_idx` to `size_t i` in for loop. If `nearest_idx` is -1, it converts to a very large positive number (e.g., 18446744073709551615 on 64-bit systems) - **Fix**: - Added validation to check `nearest_idx < 0` - Use explicit `static_cast()` for safe conversion - Return safe default (first path point) if index is invalid ### 4. **Visualization Division by Zero (qt_gui_demo.cpp)** **Issue**: Missing bounds check for scale calculation - **Location**: `examples/qt_gui_demo.cpp`, line 100 (original) - **Problem**: If all path points have identical coordinates, `range` becomes 0, causing division by zero: ```cpp double scale = std::min(width() - 2 * padding, height() - 2 * padding) / range; ``` - **Fix**: Added check for `range < 1e-6` and default to 1.0 to prevent division by zero ## Testing Recommendations 1. **Test CSV Loading with smooth_path_arc.csv**: - Verify that the GUI no longer crashes when loading the file - Check that all 150 path points are loaded correctly - Verify visualization displays the arc path properly 2. **Test Edge Cases**: - CSV files with malformed data (missing columns, invalid numbers) - Paths with degenerate cases (all points at same location) - Empty path files - CSV files with extra whitespace around values 3. **Verify Control Generation**: - Run Pure Pursuit algorithm with loaded path - Run Stanley algorithm with loaded path - Check that control sequences are generated without crashes ## Files Modified 1. `src/path_curve_custom.cpp` - CSV parsing improvements 2. `src/control_generator.cpp` - Index validation in Stanley and Pure Pursuit algorithms 3. `examples/qt_gui_demo.cpp` - Division by zero prevention in visualization ## Related Issues Prevented - **Stack overflow**: From invalid array access with large negative indices cast to unsigned - **Data corruption**: From misaligned CSV column parsing - **Graphics rendering failures**: From NaN/infinity scale values - **Segmentation faults**: From accessing out-of-bounds array indices