Skip to content

Commit 65b3da5

Browse files
committed
SDIndex: additinal checks
1 parent 452c508 commit 65b3da5

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/AudioTools/Disk/SDIndex.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)