Skip to content

Commit

Permalink
mtd: check the return value of malloc and pread
Browse files Browse the repository at this point in the history
Check the return value of malloc and pread in case they fail.

Signed-off-by: Qiyuan Zhang <[email protected]>
  • Loading branch information
Qiyuan Zhang authored and testuser7 committed Aug 12, 2024
1 parent e8ae137 commit b5d2f03
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions package/system/mtd/src/linksys_bootcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,24 @@ int mtd_resetbc(const char *mtd)
}

num_bc = mtd_info.size / bc_offset_increment;
curr = malloc(bc_offset_increment);
curr = malloc(bc_offset_increment);

if(curr == NULL) {
DLOG_ERR("Failed to allocate %u bytes from memory.", bc_offset_increment);

retval = -6;
goto out;
}

for (i = 0; i < num_bc; i++) {
pread(fd, curr, sizeof(struct bootcounter), i * bc_offset_increment);
ret = pread(fd, curr, sizeof(struct bootcounter), i * bc_offset_increment);

if(ret != sizeof(struct bootcounter)) {
DLOG_ERR("Failed to read boot-count log at offset %08x.", i * bc_offset_increment);

retval = -5;
goto out;
}

/* Existing code assumes erase is to 0xff; left as-is (2019) */
if (curr->magic == 0xffffffff)
Expand Down Expand Up @@ -179,7 +193,9 @@ int mtd_resetbc(const char *mtd)
}

out:
if (curr != NULL) free(curr);
if (curr != NULL)
free(curr);

close(fd);
return retval;
}

0 comments on commit b5d2f03

Please sign in to comment.