File tree Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Expand file tree Collapse file tree 1 file changed +20
-4
lines changed Original file line number Diff line number Diff line change @@ -56,17 +56,33 @@ class SDIndex {
56
56
57
57
// / Access file name by index
58
58
const char *operator [](int idx) {
59
- // return null when inx too big
60
- if (max_idx >= 0 && idx > max_idx ) {
61
- LOGE (" idx %d > size %d " , idx, max_idx );
59
+ // return null when idx is negative
60
+ if (idx < 0 ) {
61
+ LOGE (" idx %d is negative " , idx);
62
62
return nullptr ;
63
63
}
64
+
65
+ // return null when idx too big
66
+ if (max_idx >= 0 && idx >= max_idx) {
67
+ LOGE (" idx %d >= size %d" , idx, max_idx);
68
+ return nullptr ;
69
+ }
70
+
64
71
// find record
65
72
FileT idxfile = p_sd->open (idx_path.c_str ());
73
+
74
+ // Check if file was successfully opened
75
+ if (!idxfile) {
76
+ LOGE (" Failed to open index file: %s" , idx_path.c_str ());
77
+ return nullptr ;
78
+ }
79
+
66
80
int count = 0 ;
67
81
68
82
if (idxfile.available () == 0 ) {
69
83
LOGE (" Index file is empty" );
84
+ idxfile.close ();
85
+ return nullptr ;
70
86
}
71
87
72
88
bool found = false ;
@@ -88,7 +104,7 @@ class SDIndex {
88
104
count++;
89
105
}
90
106
if (!found) {
91
- max_idx = count;
107
+ max_idx = count - 1 ; // Fix: count represents total entries, max valid index is count-1
92
108
}
93
109
idxfile.close ();
94
110
You can’t perform that action at this time.
0 commit comments