Skip to content

Commit e0b12a8

Browse files
authored
Merge pull request #109 from stonet-research/fix-strncmp-in-segmap
Fix strncmp bug in segmap
2 parents c4f2add + fb1d79c commit e0b12a8

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

zns-tools.fs/lib/libzns-tools.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ int get_extents(char *filename, int fd, struct stat *stats) {
564564
uint64_t ext_ctr = 0;
565565
struct file_counter_map *temp = NULL;
566566

567-
fiemap = calloc(1, sizeof(struct fiemap)
568-
+ sizeof(struct fiemap_extent) * stats->st_blocks);
567+
fiemap = calloc(1, sizeof(struct fiemap) +
568+
sizeof(struct fiemap_extent) * stats->st_blocks);
569569
extent = calloc(1, sizeof(struct extent));
570570

571571
fiemap->fm_flags = FIEMAP_FLAG_SYNC;
@@ -742,7 +742,8 @@ int contains_element(uint32_t list[], uint32_t element, uint32_t size) {
742742
* */
743743
uint32_t get_file_extent_count(char *file) {
744744
for (uint32_t i = 0; i < ctrl.file_counter_map->file_ctr; i++) {
745-
if (strncmp(ctrl.file_counter_map->files[i].file, file,
745+
if (strlen(ctrl.file_counter_map->files[i].file) == strlen(file) &&
746+
strncmp(ctrl.file_counter_map->files[i].file, file,
746747
strlen(ctrl.file_counter_map->files[i].file)) == 0) {
747748
return ctrl.file_counter_map->files[i].ext_ctr;
748749
}
@@ -767,7 +768,8 @@ void increase_file_segment_counter(char *file, unsigned int num_segments,
767768
enum type type = seg_i->type;
768769

769770
for (i = 0; i < ctrl.file_counter_map->file_ctr; i++) {
770-
if (strncmp(ctrl.file_counter_map->files[i].file, file,
771+
if (strlen(ctrl.file_counter_map->files[i].file) == strlen(file) &&
772+
strncmp(ctrl.file_counter_map->files[i].file, file,
771773
strlen(ctrl.file_counter_map->files[i].file)) == 0) {
772774
goto found;
773775
}

zns-tools.fs/src/segmap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ static unsigned int get_file_stats_index(char *filename) {
275275
uint32_t i = 0;
276276

277277
for (i = 0; i < segmap_man.ctr; i++) {
278-
if (strncmp(segmap_man.fs[i].filename, filename, MAX_FILE_LENGTH) ==
279-
0) {
278+
if (strlen(segmap_man.fs[i].filename) == strlen(filename) &&
279+
strncmp(segmap_man.fs[i].filename, filename, MAX_FILE_LENGTH) ==
280+
0) {
280281
return i;
281282
}
282283
}

0 commit comments

Comments
 (0)