[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

fix clang v14 warnings in nbd-server.c



Clang 14 on macOS 12.7.4 (and I guess also newer versions) emits these warnings when compiling nbd-server.c:

  CC       nbd_server-nbd-server.o
nbd-server.c:2885:15: warning: & has lower precedence than !=; != will be evaluated first [-Wparentheses]
        if(req->type & NBD_CMD_FLAG_DF != 0) {
                     ^~~~~~~~~~~~~~~~~~~~~~
nbd-server.c:2885:15: note: place parentheses around the '!=' expression to silence this warning
        if(req->type & NBD_CMD_FLAG_DF != 0) {
                     ^ ~~~~~~~~~~~~~~~~~~~~
nbd-server.c:2885:15: note: place parentheses around the & expression to evaluate it first
        if(req->type & NBD_CMD_FLAG_DF != 0) {
           ~~~~~~~~~~^~~~~~~~~~~~~~~~~
nbd-server.c:3315:10: warning: result of comparison of constant 18446744073709551614 with expression of type 'uint32_t' (aka 'unsigned int') is always false [-Wtautological-constant-out-of-range-compare]
        if (len >= ULONG_MAX - 1) {
            ~~~ ^  ~~~~~~~~~~~~~

This commit will address these 2 warnings and fix the code.

-----

I've also opened a pull request in github for this fix:

https://github.com/NetworkBlockDevice/nbd/pull/160

Feel free to merge the PR or just apply the attached patch.


		Lars R.
From a72c1f841ba0743aeac571a72f4f4ac2886d2f9f Mon Sep 17 00:00:00 2001
From: roker <roker@pep-project.org>
Date: Sat, 30 Mar 2024 12:36:54 +0100
Subject: [PATCH] fix clang warnings in nbd-server.c

---
 nbd-server.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/nbd-server.c b/nbd-server.c
index 0ff7992..588e56b 100644
--- a/nbd-server.c
+++ b/nbd-server.c
@@ -2882,7 +2882,7 @@ static void handle_normal_read(CLIENT *client, struct nbd_request *req)
 	} else {
 		ctx->is_structured = 0;
 	}
-	if(req->type & NBD_CMD_FLAG_DF != 0) {
+	if((req->type & NBD_CMD_FLAG_DF) != 0) {
 		ctx->df = 1;
 	}
 	if(ctx->is_structured && ctx->df && req->len > (1 << 20)) {
@@ -3312,7 +3312,7 @@ static int handle_childname(GArray* servers, int socket)
 				break;
 		}
 	}
-	if (len >= ULONG_MAX - 1) {
+	if (len >= UINT32_MAX - 1) {
 		err_nonfatal("Value out of range");
 		return -1;
 	}
-- 
2.44.0

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


Reply to: