Skip to contentSkip to conversation

Avoid passing NULL pointer to printf

PATCH New Apollon Oikonomopoulos 1 participant 3 messages Oct 20, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Avoid passing NULL pointer to printf

iscsi_add_portal sometimes passes a NULL pointer as a %s argument to printf. This is was already considered an error when using -Werror=format-overflow, but it is only since GCC 10 that GCC is able to detect this particular case:

In file included from iscsi/iscsid.h:29,

                  from iscsi/iscsi_tcp.c:35:
 In function 'iscsi_add_portal',
     inlined from 'iscsi_add_portal' at iscsi/iscsi_tcp.c:408:5,
     inlined from 'iscsi_tcp_init' at iscsi/iscsi_tcp.c:449:3:
 ./log.h:90:2: error: '%s' directive argument is null [-Werror=format-overflow=]
    90 |  log_error("%s(%d) " fmt, __FUNCTION__, __LINE__, ##args); \
       |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 iscsi/iscsi_tcp.c:411:3: note: in expansion of macro 'eprintf'
   411 |   eprintf("failed to create/bind to portal %s:%d\n", addr, port);
       |   ^~~~~~~

Fix this by passing the empty string to eprintf() if addr is NULL.

---
 usr/iscsi/iscsi_tcp.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
usr/iscsi/iscsi_tcp.c +7 -1
index 2bb8356..39dfb71 100644
@@ -407,8 +407,14 @@ int iscsi_tcp_init_portal(char *addr, int port, int tpgt)
 
 int iscsi_add_portal(char *addr, int port, int tpgt)
 {
+	const char *addr_str = "";
+
 	if (iscsi_tcp_init_portal(addr, port, tpgt)) {
-		eprintf("failed to create/bind to portal %s:%d\n", addr, port);
+		if (addr) {
+			addr_str = addr;
+		}
+		eprintf("failed to create/bind to portal %s:%d\n",
+			addr_str, port);
 		return -1;
 	}
 
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/1 Replace __packed with __attribute__(...)

GCC 10 changed the default from -fcommon to -fno-common. This makes the tgt build fail because of multiple definitions of __packed. Change all instances of __packed to __attribute__((__packed__)), which is the canonical attribute form.

---
 usr/iscsi/iscsid.c | 2 +-
 usr/iscsi/iser.c   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
usr/iscsi/iscsid.c +1 -1
index a54a46e..476a760 100644
@@ -1074,7 +1074,7 @@ void iscsi_rsp_set_residual(struct iscsi_cmd_rsp *rsp, struct scsi_cmd *scmd)
 struct iscsi_sense_data {
 	uint16_t length;
 	uint8_t  data[0];
-} __packed;
+} __attribute__((__packed__));
 
 static int iscsi_cmd_rsp_build(struct iscsi_task *task)
 {
usr/iscsi/iser.c +1 -1
index c7c43b2..2abb872 100644
@@ -92,7 +92,7 @@ char *iser_portal_addr;
 struct iscsi_sense_data {
 	uint16_t length;
 	uint8_t data[0];
-} __packed;
+} __attribute__((__packed__));
 
 static size_t buf_pool_sz_mb = DEFAULT_POOL_SIZE_MB;
 static int cq_vector = -1;
Signature · 1 line
2.28.0
#2 lore raw

PATCH 1/1 scripts/checkpatch.pl: escape literal left braces

In reply to Apollon Oikonomopoulos

Since Perl 5.22.0, left braces in regexps should be escaped.

---
 scripts/checkpatch.pl | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
scripts/checkpatch.pl +5 -5
index b1281ac..30fc77f 100755
@@ -2397,8 +2397,8 @@ sub process {
 
 # function brace can't be on same line, except for #defines of do while,
 # or if closed on same line
-		if (($line=~/$Type\s*$Ident\(.*\).*\s{/) and
-		    !($line=~/\#\s*define.*do\s{/) and !($line=~/}/)) {
+		if (($line=~/$Type\s*$Ident\(.*\).*\s\{/) and
+		    !($line=~/\#\s*define.*do\s\{/) and !($line=~/\}/)) {
 			ERROR("OPEN_BRACE",
 			      "open brace '{' following function declarations go on the next line\n" . $herecurr);
 		}
@@ -2673,8 +2673,8 @@ sub process {
 ## 		}
 
 #need space before brace following if, while, etc
-		if (($line =~ /\(.*\){/ && $line !~ /\($Type\){/) ||
-		    $line =~ /do{/) {
+		if (($line =~ /\(.*\)\{/ && $line !~ /\($Type\){/) ||
+		    $line =~ /do\{/) {
 			ERROR("SPACING",
 			      "space required before the open brace '{'\n" . $herecurr);
 		}
@@ -2964,7 +2964,7 @@ sub process {
 			    $dstat !~ /^for\s*$Constant$/ &&				# for (...)
 			    $dstat !~ /^for\s*$Constant\s+(?:$Ident|-?$Constant)$/ &&	# for (...) bar()
 			    $dstat !~ /^do\s*{/ &&					# do {...
-			    $dstat !~ /^\({/)						# ({...
+			    $dstat !~ /^\(\{/)						# ({...
 			{
 				$ctx =~ s/\n*$//;
 				my $herectx = $here . "\n";
Signature · 1 line
2.28.0