Skip to content

Commit ae24cf1

Browse files
committed
hw/uefi: fix error handling in uefi_vars_json_save
Catch lseek errors. Return on errors. Use autoptr for the GString to simplify cleanup. Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Signed-off-by: Gerd Hoffmann <[email protected]> Message-ID: <[email protected]>
1 parent 560429f commit ae24cf1

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

hw/uefi/var-service-json.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ void uefi_vars_json_init(uefi_vars_state *uv, Error **errp)
178178

179179
void uefi_vars_json_save(uefi_vars_state *uv)
180180
{
181-
GString *gstr;
181+
g_autoptr(GString) gstr = NULL;
182182
int rc;
183183

184184
if (uv->jsonfd == -1) {
@@ -187,18 +187,25 @@ void uefi_vars_json_save(uefi_vars_state *uv)
187187

188188
gstr = uefi_vars_to_json(uv);
189189

190-
lseek(uv->jsonfd, 0, SEEK_SET);
190+
rc = lseek(uv->jsonfd, 0, SEEK_SET);
191+
if (rc < 0) {
192+
warn_report("%s: lseek error", __func__);
193+
return;
194+
}
195+
191196
rc = ftruncate(uv->jsonfd, 0);
192197
if (rc != 0) {
193198
warn_report("%s: ftruncate error", __func__);
199+
return;
194200
}
201+
195202
rc = write(uv->jsonfd, gstr->str, gstr->len);
196203
if (rc != gstr->len) {
197204
warn_report("%s: write error", __func__);
205+
return;
198206
}
199-
fsync(uv->jsonfd);
200207

201-
g_string_free(gstr, true);
208+
fsync(uv->jsonfd);
202209
}
203210

204211
void uefi_vars_json_load(uefi_vars_state *uv, Error **errp)

0 commit comments

Comments
 (0)