Some code cleanups

Andreas Schneider asn at samba.org
Tue Feb 13 11:31:28 UTC 2018


Hi,

some patches for cleaning up code while looking into it.


Please review and push if OK.


Thanks,


	Andreas

-- 
Andreas Schneider                   GPG-ID: CC014E3D
Samba Team                             asn at samba.org
www.samba.org
-------------- next part --------------
>From 498c065ccef799b3eea02aef721cc8f04f7c9e0e Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Thu, 26 Oct 2017 09:47:57 +0200
Subject: [PATCH 1/3] util: Add sanity check for count in ms_fnmatch_protocol()

We need to initialize `ret` in case the 'if' condition is false.

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 lib/util/ms_fnmatch.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/util/ms_fnmatch.c b/lib/util/ms_fnmatch.c
index c0f61ab04e7..5ffea75252c 100644
--- a/lib/util/ms_fnmatch.c
+++ b/lib/util/ms_fnmatch.c
@@ -164,7 +164,7 @@ static int ms_fnmatch_core(const char *p, const char *n,
 int ms_fnmatch_protocol(const char *pattern, const char *string, int protocol,
 			bool is_case_sensitive)
 {
-	int ret, count, i;
+	int ret = -1, count, i;
 
 	if (strcmp(string, "..") == 0) {
 		string = ".";
@@ -209,7 +209,7 @@ int ms_fnmatch_protocol(const char *pattern, const char *string, int protocol,
 		if (pattern[i] == '*' || pattern[i] == '<') count++;
 	}
 
-	{
+	if (count > 0) {
 		struct max_n max_n[count];
 
 		memset(max_n, 0, sizeof(struct max_n) * count);
-- 
2.16.1


>From 3b5ab69d716a482f2776fc1aa5a62be86392cf78 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Fri, 5 Jan 2018 10:43:18 +0100
Subject: [PATCH 2/3] smbspool: Initialize empty_str on declaration

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 source3/client/smbspool.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/source3/client/smbspool.c b/source3/client/smbspool.c
index 3b732c99234..949cdb4076e 100644
--- a/source3/client/smbspool.c
+++ b/source3/client/smbspool.c
@@ -94,15 +94,13 @@ main(int argc,			/* I - Number of command-line arguments */
 	FILE           *fp;	/* File to print */
 	int             status = 1;	/* Status of LPD job */
 	struct cli_state *cli;	/* SMB interface */
-	char            null_str[1];
+	char            empty_str[] = "";
 	int             tries = 0;
 	bool		need_auth = true;
 	const char     *dev_uri;
 	const char     *config_file = NULL;
 	TALLOC_CTX     *frame = talloc_stackframe();
 
-	null_str[0] = '\0';
-
 	if (argc == 1) {
 		/*
 	         * NEW!  In CUPS 1.1 the backends are run with no arguments
@@ -190,16 +188,16 @@ main(int argc,			/* I - Number of command-line arguments */
 			*tmp2++ = '\0';
 			password = uri_unescape_alloc(tmp2);
 		} else {
-			password = null_str;
+			password = empty_str;
 		}
 		username = uri_unescape_alloc(tmp);
 	} else {
 		if ((username = getenv("AUTH_USERNAME")) == NULL) {
-			username = null_str;
+			username = empty_str;
 		}
 
 		if ((password = getenv("AUTH_PASSWORD")) == NULL) {
-			password = null_str;
+			password = empty_str;
 		}
 
 		server = uri + 6;
-- 
2.16.1


>From 681f4aa709684c3a2fd134409dbd4c97a54d84cd Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Fri, 5 Jan 2018 10:50:57 +0100
Subject: [PATCH 3/3] smbspool: Improve URI handling code

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 source3/client/smbspool.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/source3/client/smbspool.c b/source3/client/smbspool.c
index 949cdb4076e..68f5e6d0716 100644
--- a/source3/client/smbspool.c
+++ b/source3/client/smbspool.c
@@ -100,6 +100,7 @@ main(int argc,			/* I - Number of command-line arguments */
 	const char     *dev_uri;
 	const char     *config_file = NULL;
 	TALLOC_CTX     *frame = talloc_stackframe();
+	int cmp;
 
 	if (argc == 1) {
 		/*
@@ -153,20 +154,20 @@ main(int argc,			/* I - Number of command-line arguments */
 	}
 
 	/*
-         * Find the URI...
-         */
-
+	 * Find the URI ...
+	 */
 	dev_uri = getenv("DEVICE_URI");
-	if (dev_uri) {
-		strncpy(uri, dev_uri, sizeof(uri) - 1);
-	} else if (strncmp(argv[1], "smb://", 6) == 0) {
-		strncpy(uri, argv[1], sizeof(uri) - 1);
-	} else {
-		fputs("ERROR: No device URI found in DEVICE_URI environment variable or arg1 !\n", stderr);
-		goto done;
+	if (dev_uri == NULL || strlen(dev_uri) == 0) {
+		dev_uri = argv[1];
 	}
 
-	uri[sizeof(uri) - 1] = '\0';
+	cmp = strncmp(dev_uri, "smb://", 6);
+	if (cmp != 0) {
+		fprintf(stderr,
+			"ERROR: No valid device URI has been specified\n");
+		goto done;
+	}
+	snprintf(uri, sizeof(uri), "%s", dev_uri);
 
 	/*
          * Extract the destination from the URI...
-- 
2.16.1



More information about the samba-technical mailing list