Skip to content

Commit 31d3ba9

Browse files
committed
rimage: fix file operations error checks
This will fix file operations Signed-off-by: Adrian Bonislawski <[email protected]>
1 parent 9d45332 commit 31d3ba9

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/ext_manifest.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const struct ext_man_header ext_man_template = {
2525
static int ext_man_open_file(struct image *image)
2626
{
2727
/* open extended manifest outfile for writing */
28-
sprintf(image->out_ext_man_file, "%s.xman", image->out_file);
28+
snprintf(image->out_ext_man_file, sizeof(image->out_ext_man_file),
29+
"%s.xman", image->out_file);
2930
unlink(image->out_ext_man_file);
3031

3132
image->out_ext_man_fd = fopen(image->out_ext_man_file, "wb");

src/file_simple.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ static int simple_write_module(struct image *image, struct module *module)
128128

129129
/* Get the pointer of writing hdr */
130130
ptr_hdr = ftell(image->out_fd);
131+
if (ptr_hdr < 0)
132+
return file_error("cant get file position", image->out_file);
131133
count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
132134
if (count != 1) {
133135
fprintf(stderr, "error: failed to write section header %d\n",
@@ -180,7 +182,9 @@ static int simple_write_module(struct image *image, struct module *module)
180182
-errno);
181183
return -errno;
182184
}
183-
fseek(image->out_fd, ptr_cur, SEEK_SET);
185+
err = fseek(image->out_fd, ptr_cur, SEEK_SET);
186+
if (err)
187+
return file_error("cant seek", image->out_file);
184188

185189
fprintf(stdout, "\n");
186190
/* return padding size */
@@ -324,7 +328,10 @@ int simple_write_firmware(struct image *image)
324328
hdr.file_size += ret;
325329
}
326330
/* overwrite hdr */
327-
fseek(image->out_fd, 0, SEEK_SET);
331+
ret = fseek(image->out_fd, 0, SEEK_SET);
332+
if (ret)
333+
return file_error("can't seek set", image->out_file);
334+
328335
count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
329336
if (count != 1)
330337
return -errno;

src/manifest.c

+10-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ static int man_open_rom_file(struct image *image)
2727
{
2828
uint32_t size;
2929

30-
sprintf(image->out_rom_file, "%s.rom", image->out_file);
30+
snprintf(image->out_rom_file, sizeof(image->out_rom_file),
31+
"%s.rom", image->out_file);
3132
unlink(image->out_rom_file);
3233

3334
size = image->adsp->mem_zones[SOF_FW_BLK_TYPE_ROM].size;
@@ -67,7 +68,8 @@ static int man_open_unsigned_file(struct image *image)
6768
static int man_open_manifest_file(struct image *image)
6869
{
6970
/* open manifest outfile for writing */
70-
sprintf(image->out_man_file, "%s.met", image->out_file);
71+
snprintf(image->out_man_file, sizeof(image->out_man_file),
72+
"%s.met", image->out_file);
7173
unlink(image->out_man_file);
7274

7375
image->out_man_fd = fopen(image->out_man_file, "wb");
@@ -1361,10 +1363,10 @@ int man_write_fw_v2_5(struct image *image)
13611363
int verify_image(struct image *image)
13621364
{
13631365
FILE *in_file;
1364-
int ret, i;
1366+
int ret = 0;
13651367
long size;
13661368
void *buffer;
1367-
size_t read;
1369+
size_t read, i;
13681370

13691371
/* is verify supported for target ? */
13701372
if (!image->adsp->verify_firmware) {
@@ -1432,15 +1434,16 @@ int verify_image(struct image *image)
14321434
image->verify_file);
14331435
out:
14341436
fclose(in_file);
1435-
return 0;
1437+
return ret;
14361438
}
14371439

14381440

14391441
int resign_image(struct image *image)
14401442
{
14411443
int key_size, key_file_size;
14421444
void *buffer = NULL;
1443-
size_t size, read;
1445+
size_t read;
1446+
int32_t size;
14441447
FILE *in_file;
14451448
int ret, i;
14461449

@@ -1487,7 +1490,7 @@ int resign_image(struct image *image)
14871490
/* read file into buffer */
14881491
read = fread(buffer, 1, size, in_file);
14891492
if (read != size) {
1490-
fprintf(stderr, "error: unable to read %zu bytes from %s err %d\n",
1493+
fprintf(stderr, "error: unable to read %d bytes from %s err %d\n",
14911494
size, image->in_file, errno);
14921495
ret = errno;
14931496
goto out;

0 commit comments

Comments
 (0)