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 {
5656
5757 // / Access file name by index
5858 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);
6262 return nullptr ;
6363 }
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+
6471 // find record
6572 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+
6680 int count = 0 ;
6781
6882 if (idxfile.available () == 0 ) {
6983 LOGE (" Index file is empty" );
84+ idxfile.close ();
85+ return nullptr ;
7086 }
7187
7288 bool found = false ;
@@ -88,7 +104,7 @@ class SDIndex {
88104 count++;
89105 }
90106 if (!found) {
91- max_idx = count;
107+ max_idx = count - 1 ; // Fix: count represents total entries, max valid index is count-1
92108 }
93109 idxfile.close ();
94110
You can’t perform that action at this time.
0 commit comments