public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
From: Bastien Nocera <hadess@hadess.net>
To: linux-bluetooth@vger.kernel.org
Cc: Bastien Nocera <hadess@hadess.net>
Subject: [BlueZ v2 03/20] client/gatt: Don't pass negative fd on error
Date: Fri, 10 May 2024 14:10:13 +0200	[thread overview]
Message-ID: <20240510121355.3241456-4-hadess@hadess.net> (raw)
In-Reply-To: <20240510121355.3241456-1-hadess@hadess.net>

Error: NEGATIVE_RETURNS (CWE-394): [#def33]
bluez-5.75/client/gatt.c:973:2: negative_return_fn: Function "io_get_fd(io)" returns a negative number.
bluez-5.75/client/gatt.c:973:2: negative_returns: "io_get_fd(io)" is passed to a parameter that cannot be negative.
971|	msg.msg_iovlen = iovlen;
972|
973|->	ret = sendmsg(io_get_fd(io), &msg, MSG_NOSIGNAL);
974|	if (ret < 0) {
975|		ret = -errno;

Error: NEGATIVE_RETURNS (CWE-394): [#def34]
bluez-5.75/client/gatt.c:1049:2: negative_return_fn: Function "io_get_fd(io)" returns a negative number.
bluez-5.75/client/gatt.c:1049:2: assign: Assigning: "fd" = "io_get_fd(io)".
bluez-5.75/client/gatt.c:1062:2: negative_returns: "fd" is passed to a parameter that cannot be negative.
1060|		msg.msg_iovlen = 1;
1061|
1062|->		bytes_read = recvmsg(fd, &msg, MSG_DONTWAIT);
1063|		if (bytes_read < 0) {
1064|			bt_shell_printf("recvmsg: %s", strerror(errno));
---
 client/gatt.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/client/gatt.c b/client/gatt.c
index 3aaa7a9361b9..6c7603985172 100644
--- a/client/gatt.c
+++ b/client/gatt.c
@@ -966,11 +966,15 @@ static int sock_send(struct io *io, struct iovec *iov, size_t iovlen)
 	struct msghdr msg;
 	int ret;
 
+	ret = io_get_fd(io);
+	if (ret < 0)
+		return ret;
+
 	memset(&msg, 0, sizeof(msg));
 	msg.msg_iov = iov;
 	msg.msg_iovlen = iovlen;
 
-	ret = sendmsg(io_get_fd(io), &msg, MSG_NOSIGNAL);
+	ret = sendmsg(ret, &msg, MSG_NOSIGNAL);
 	if (ret < 0) {
 		ret = -errno;
 		bt_shell_printf("sendmsg: %s", strerror(-ret));
@@ -1052,6 +1056,11 @@ static bool sock_read(struct io *io, void *user_data)
 	if (io != notify_io.io && !chrc)
 		return true;
 
+	if (fd < 0) {
+		bt_shell_printf("recvmsg: %s", strerror(-fd));
+		return false;
+	}
+
 	iov.iov_base = buf;
 	iov.iov_len = sizeof(buf);
 
-- 
2.44.0


  parent reply	other threads:[~2024-05-10 12:14 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-10 12:10 [BlueZ v2 00/20] Fix a number of static analysis issues Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 01/20] adapter: Use false instead of 0 for bool Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 02/20] attrib/gatt: Guard against possible integer overflow Bastien Nocera
2024-05-10 12:10 ` Bastien Nocera [this message]
2024-05-10 12:10 ` [BlueZ v2 04/20] client/gatt: Check write_value() retval Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 05/20] client/main: Fix array access Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 06/20] client/main: Fix mismatched free Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 07/20] monitor/att: Fix memory leak Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 08/20] bap: Fix memory leaks Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 09/20] media: Fix memory leak Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 10/20] main: Fix memory leaks Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 11/20] isotest: Consider "0" fd to be valid Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 12/20] isotest: Fix error check after opening file Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 13/20] client/player: Fix copy/paste error Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 14/20] shared/vcp: " Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 15/20] isotest: Fix fd leak Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 16/20] iso-tester: " Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 17/20] sdp: Fix use of uninitialised memory Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 18/20] monitor: Work-around memory leak warning Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 19/20] avrcp: Fix uninitialised memory usage Bastien Nocera
2024-05-10 12:10 ` [BlueZ v2 20/20] main: Simplify variable assignment Bastien Nocera
2024-05-10 15:40 ` [BlueZ v2 00/20] Fix a number of static analysis issues patchwork-bot+bluetooth
2024-05-10 16:42   ` Luiz Augusto von Dentz
2024-05-14 10:05     ` Bastien Nocera

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240510121355.3241456-4-hadess@hadess.net \
    --to=hadess@hadess.net \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox