Skip to content

Commit d287016

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 d287016

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
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

+26-3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ 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+
fprintf(stderr, "error: cant get file position\n");
133+
return -errno;
134+
}
135+
131136
count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
132137
if (count != 1) {
133138
fprintf(stderr, "error: failed to write section header %d\n",
@@ -172,15 +177,28 @@ static int simple_write_module(struct image *image, struct module *module)
172177
hdr.size += padding;
173178
/* Record current pointer, will set it back after overwriting hdr */
174179
ptr_cur = ftell(image->out_fd);
180+
if (ptr_cur < 0) {
181+
fprintf(stderr, "error: cant get file position\n");
182+
return -errno;
183+
}
175184
/* overwrite hdr */
176-
fseek(image->out_fd, ptr_hdr, SEEK_SET);
185+
err = fseek(image->out_fd, ptr_hdr, SEEK_SET);
186+
if (err) {
187+
fprintf(stderr, "cant seek %d\n", -errno);
188+
return -errno;
189+
}
190+
177191
count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
178192
if (count != 1) {
179193
fprintf(stderr, "error: failed to write section header %d\n",
180194
-errno);
181195
return -errno;
182196
}
183-
fseek(image->out_fd, ptr_cur, SEEK_SET);
197+
err = fseek(image->out_fd, ptr_cur, SEEK_SET);
198+
if (err) {
199+
fprintf(stderr, "cant seek %d\n", -errno);
200+
return -errno;
201+
}
184202

185203
fprintf(stdout, "\n");
186204
/* return padding size */
@@ -324,7 +342,12 @@ int simple_write_firmware(struct image *image)
324342
hdr.file_size += ret;
325343
}
326344
/* overwrite hdr */
327-
fseek(image->out_fd, 0, SEEK_SET);
345+
ret = fseek(image->out_fd, 0, SEEK_SET);
346+
if (ret) {
347+
fprintf(stderr, "cant seek %d\n", -errno);
348+
return -errno;
349+
}
350+
328351
count = fwrite(&hdr, sizeof(hdr), 1, image->out_fd);
329352
if (count != 1)
330353
return -errno;

src/manifest.c

+12-8
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;
@@ -50,7 +51,8 @@ static int man_open_rom_file(struct image *image)
5051

5152
static int man_open_unsigned_file(struct image *image)
5253
{
53-
sprintf(image->out_unsigned_file, "%s.uns", image->out_file);
54+
snprintf(image->out_unsigned_file, sizeof(image->out_unsigned_file),
55+
"%s.uns", image->out_file);
5456
unlink(image->out_unsigned_file);
5557

5658
/* open unsigned FW outfile for writing */
@@ -67,7 +69,8 @@ static int man_open_unsigned_file(struct image *image)
6769
static int man_open_manifest_file(struct image *image)
6870
{
6971
/* open manifest outfile for writing */
70-
sprintf(image->out_man_file, "%s.met", image->out_file);
72+
snprintf(image->out_man_file, sizeof(image->out_man_file),
73+
"%s.met", image->out_file);
7174
unlink(image->out_man_file);
7275

7376
image->out_man_fd = fopen(image->out_man_file, "wb");
@@ -1361,10 +1364,10 @@ int man_write_fw_v2_5(struct image *image)
13611364
int verify_image(struct image *image)
13621365
{
13631366
FILE *in_file;
1364-
int ret, i;
1367+
int ret = 0;
13651368
long size;
13661369
void *buffer;
1367-
size_t read;
1370+
size_t read, i;
13681371

13691372
/* is verify supported for target ? */
13701373
if (!image->adsp->verify_firmware) {
@@ -1432,15 +1435,16 @@ int verify_image(struct image *image)
14321435
image->verify_file);
14331436
out:
14341437
fclose(in_file);
1435-
return 0;
1438+
return ret;
14361439
}
14371440

14381441

14391442
int resign_image(struct image *image)
14401443
{
14411444
int key_size, key_file_size;
14421445
void *buffer = NULL;
1443-
size_t size, read;
1446+
size_t read;
1447+
int32_t size;
14441448
FILE *in_file;
14451449
int ret, i;
14461450

@@ -1487,7 +1491,7 @@ int resign_image(struct image *image)
14871491
/* read file into buffer */
14881492
read = fread(buffer, 1, size, in_file);
14891493
if (read != size) {
1490-
fprintf(stderr, "error: unable to read %zu bytes from %s err %d\n",
1494+
fprintf(stderr, "error: unable to read %d bytes from %s err %d\n",
14911495
size, image->in_file, errno);
14921496
ret = errno;
14931497
goto out;

src/rimage.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ int main(int argc, char *argv[])
160160

161161
if (image.in_file) {
162162
fprintf(stdout, "going to re-sign\n");
163-
return resign_image(&image);
163+
ret = resign_image(&image);
164+
goto out;
164165
}
165166

166167
/* set IMR Type in found machine definition */

0 commit comments

Comments
 (0)