From d5d311f026f69c56a409d856f5e11cdff0526c6c Mon Sep 17 00:00:00 2001 From: Chris Plummer Date: Tue, 29 Jul 2025 19:43:13 +0000 Subject: [PATCH] 8361873: [GCC static analyzer] exec_md.c forkedChildProcess potential double 'close' of file descriptor '3' Reviewed-by: jpai, stuefe, mbaesken --- src/jdk.jdwp.agent/unix/native/libjdwp/exec_md.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/jdk.jdwp.agent/unix/native/libjdwp/exec_md.c b/src/jdk.jdwp.agent/unix/native/libjdwp/exec_md.c index a0f2687a88c..096a70fe524 100644 --- a/src/jdk.jdwp.agent/unix/native/libjdwp/exec_md.c +++ b/src/jdk.jdwp.agent/unix/native/libjdwp/exec_md.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -77,7 +77,10 @@ closeDescriptors(void) * the lowest numbered file descriptor, just like open(). So * before calling opendir(), we close a couple explicitly, so that * opendir() can then use these lowest numbered closed file - * descriptors afresh. */ + * descriptors afresh. + * + * WARNING: We are not allowed to return with a failure until after + * these two closes are done. forkedChildProcess() relies on this. */ close(from_fd); /* for possible use by opendir() */ close(from_fd + 1); /* another one for good luck */ @@ -128,8 +131,10 @@ forkedChildProcess(const char *file, char *const argv[]) JDI_ASSERT(max_fd != (rlim_t)-1); // -1 represents error /* close(), that we subsequently call, takes only int values */ JDI_ASSERT(max_fd <= INT_MAX); - /* leave out standard input/output/error file descriptors */ - rlim_t i = STDERR_FILENO + 1; + /* Leave out standard input/output/error file descriptors. Also, + * leave out STDERR_FILENO +1 and +2 since closeDescriptors() + * already closed them, even when returning an error. */ + rlim_t i = STDERR_FILENO + 3; ERROR_MESSAGE(("failed to close file descriptors of" " child process optimally, falling back to closing" " %d file descriptors sequentially", (max_fd - i + 1)));