Skip to contentSkip to conversation

erofs-utils: lib: Restore memory address before free()

PATCH In review Sandeep Dhavale via Linux-erofs 2 participants 3 messages Sep 19, 2023 – Sep 20, 2023 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: Restore memory address before free()

We move idx pointer as we iterate through for loop based on count. If we error out from the loop, restore the pointer to allocated memory before calling free().

Fixes 39147b48b76d ("erofs-utils: lib: add erofs_rebuild_load_tree() helper")
---
 lib/rebuild.c | 1 +
 1 file changed, 1 insertion(+)
lib/rebuild.c +1 -0
index 27a1df4..8739c53 100644
@@ -188,6 +188,7 @@ static int erofs_rebuild_fixup_inode_index(struct erofs_inode *inode)
 	inode->u.chunkformat |= chunkbits - sbi.blkszbits;
 	return 0;
 err:
+	idx = inode->chunkindexes;
 	free(idx);
 	inode->chunkindexes = NULL;
 	return ret;
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
#1 lore raw

Hi Sandeep,

Quoted 6 lines · Sandeep Dhavale

We move idx pointer as we iterate through for loop based on count. If we error out from the loop, restore the pointer to allocated memory before calling free().

Fixes 39147b48b76d ("erofs-utils: lib: add erofs_rebuild_load_tree() helper")

Thanks for the report!

Quoted 9 lines
---
  lib/rebuild.c | 1 +
  1 file changed, 1 insertion(+)
lib/rebuild.c +0 -0
index 27a1df4..8739c53 100644

lib/rebuild.c static int erofs_rebuild_fixup_inode_index(struct erofs_inode *inode) · line 192

Quoted 5 lines
lib/rebuild.c +1 -0
@@ -188,6 +188,7 @@ static int erofs_rebuild_fixup_inode_index(struct erofs_inode *inode)
  	inode->u.chunkformat |= chunkbits - sbi.blkszbits;
  	return 0;
  err:
+	idx = inode->chunkindexes;
  	free(idx);

I think we could just

	free(inode->chunkindexes);
	inode->chunkindexes = NULL;

I will apply like this directly.

Thanks,
Gao Xiang

Quoted 2 lines
lib/rebuild.c +0 -0
  	inode->chunkindexes = NULL;
  	return ret;
#2 lore raw
Quoted 30 lines · Gao Xiang

Hi Sandeep,

Quoted 6 lines · Sandeep Dhavale

We move idx pointer as we iterate through for loop based on count. If we error out from the loop, restore the pointer to allocated memory before calling free().

Fixes 39147b48b76d ("erofs-utils: lib: add erofs_rebuild_load_tree() helper")

Thanks for the report!

Quoted 9 lines
---
  lib/rebuild.c | 1 +
  1 file changed, 1 insertion(+)
lib/rebuild.c +0 -0
index 27a1df4..8739c53 100644

lib/rebuild.c static int erofs_rebuild_fixup_inode_index(struct erofs_inode *inode) · line 192

Quoted 5 lines
lib/rebuild.c +1 -0
@@ -188,6 +188,7 @@ static int erofs_rebuild_fixup_inode_index(struct erofs_inode *inode)
      inode->u.chunkformat |= chunkbits - sbi.blkszbits;
      return 0;
  err:
+     idx = inode->chunkindexes;
      free(idx);

I think we could just

Quoted 5 lines
        free(inode->chunkindexes);
        inode->chunkindexes = NULL;

I will apply like this directly.

Hi Gao,
Yes, this looks much better.

Thanks,
Sandeep.

Quoted 5 lines

Thanks,
Gao Xiang

Quoted 2 lines
lib/rebuild.c +0 -0
      inode->chunkindexes = NULL;
      return ret;