[PATCH 1/2] linux-user/gen-vdso: Handle fseek() failure

Peter Maydell posted 2 patches 5 months, 1 week ago
Maintainers: Laurent Vivier <laurent@vivier.eu>
[PATCH 1/2] linux-user/gen-vdso: Handle fseek() failure
Posted by Peter Maydell 5 months, 1 week ago
Coverity points out that we don't check for fseek() failure in gen-vdso.c,
and so we might pass -1 to malloc(). Add the error checking.

(This is a standalone executable that doesn't link against glib, so
we can't do the easy thing and use g_file_get_contents().)

Coverity: CID 1523742
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 linux-user/gen-vdso.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/linux-user/gen-vdso.c b/linux-user/gen-vdso.c
index fce9d5cbc3c..1c406d1b10f 100644
--- a/linux-user/gen-vdso.c
+++ b/linux-user/gen-vdso.c
@@ -113,9 +113,16 @@ int main(int argc, char **argv)
      * We expect the vdso to be small, on the order of one page,
      * therefore we do not expect a partial read.
      */
-    fseek(inf, 0, SEEK_END);
+    if (fseek(inf, 0, SEEK_END) < 0) {
+        goto perror_inf;
+    }
     total_len = ftell(inf);
-    fseek(inf, 0, SEEK_SET);
+    if (total_len < 0) {
+        goto perror_inf;
+    }
+    if (fseek(inf, 0, SEEK_SET) < 0) {
+        goto perror_inf;
+    }
 
     buf = malloc(total_len);
     if (buf == NULL) {
-- 
2.43.0
Re: [PATCH 1/2] linux-user/gen-vdso: Handle fseek() failure
Posted by Richard Henderson 5 months, 1 week ago
On 7/10/25 11:07, Peter Maydell wrote:
> Coverity points out that we don't check for fseek() failure in gen-vdso.c,
> and so we might pass -1 to malloc(). Add the error checking.
> 
> (This is a standalone executable that doesn't link against glib, so
> we can't do the easy thing and use g_file_get_contents().)
> 
> Coverity: CID 1523742
> Signed-off-by: Peter Maydell<peter.maydell@linaro.org>
> ---
>   linux-user/gen-vdso.c | 11 +++++++++--
>   1 file changed, 9 insertions(+), 2 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~