Blame SOURCES/0019-clang-more-Memory-leaks.patch

398fc5
From 0dc90f1783981ac11c3c067c40df88d6315911a6 Mon Sep 17 00:00:00 2001
398fc5
From: Rob Crittenden <rcritten@redhat.com>
398fc5
Date: Mon, 8 Oct 2018 12:53:57 -0400
398fc5
Subject: [PATCH 19/25] clang: more Memory leaks
398fc5
398fc5
Fix leaks in tests/tools/addcinfo.c, dogtag.c and submit-x.c
398fc5
---
398fc5
 src/dogtag.c           | 17 +++++++++++++----
398fc5
 src/getcert.c          |  3 ++-
398fc5
 src/store-files.c      |  1 +
398fc5
 src/submit-d.c         |  6 ++++++
398fc5
 src/submit-x.c         | 39 ++++++++++-----------------------------
398fc5
 tests/tools/addcinfo.c |  8 +++++---
398fc5
 6 files changed, 37 insertions(+), 37 deletions(-)
398fc5
398fc5
diff --git a/src/dogtag.c b/src/dogtag.c
398fc5
index 55607f3d..8e3890a5 100644
398fc5
--- a/src/dogtag.c
398fc5
+++ b/src/dogtag.c
398fc5
@@ -117,7 +117,7 @@ main(int argc, const char **argv)
398fc5
 	const char *ssldir = NULL, *cainfo = NULL, *capath = NULL;
398fc5
 	const char *sslcert = NULL, *sslkey = NULL;
398fc5
 	const char *sslpin = NULL, *sslpinfile = NULL;
398fc5
-	const char *csr = NULL, *serial = NULL, *template = NULL;
398fc5
+	const char *csr = NULL, *csre = NULL, *serial = NULL, *template = NULL;
398fc5
 	const char *uid = NULL, *pwd = NULL, *pwdfile = NULL;
398fc5
 	const char *udn = NULL, *pin = NULL, *pinfile = NULL;
398fc5
 	char *poptarg;
398fc5
@@ -127,7 +127,7 @@ main(int argc, const char **argv)
398fc5
 	} *aoptions = NULL, *soptions = NULL;
398fc5
 	size_t num_aoptions = 0, num_soptions = 0, j;
398fc5
 	char *savedstate = NULL;
398fc5
-	char *p, *q, *params = NULL, *params2 = NULL;
398fc5
+	char *p = NULL, *q = NULL, *params = NULL, *params2 = NULL;
398fc5
 	const char *lasturl = NULL, *lastparams = NULL;
398fc5
 	const char *tmp = NULL, *results = NULL;
398fc5
 	struct cm_submit_h_context *hctx;
398fc5
@@ -537,16 +537,19 @@ main(int argc, const char **argv)
398fc5
 				}
398fc5
 				poptPrintUsage(pctx, stdout, 0);
398fc5
 				free(csr);
398fc5
+				free(p);
398fc5
 				return CM_SUBMIT_STATUS_UNCONFIGURED;
398fc5
 			}
398fc5
-			csr = cm_submit_u_url_encode(csr);
398fc5
+			csre = cm_submit_u_url_encode(csr);
398fc5
 			params = talloc_asprintf(ctx,
398fc5
 						 "profileId=%s&"
398fc5
 						 "cert_request_type=pkcs10&"
398fc5
 						 "cert_request=%s&"
398fc5
 						 "xml=true",
398fc5
 						 template,
398fc5
-						 csr);
398fc5
+						 csre);
398fc5
+			free(csr);
398fc5
+			free(csre);
398fc5
 		}
398fc5
 		/* Check for creds specified as options. */
398fc5
 		for (j = 0; j < num_soptions; j++) {
398fc5
@@ -608,12 +611,16 @@ main(int argc, const char **argv)
398fc5
 			printf(_("No agent URL (-A) given, and no default "
398fc5
 				 "known.\n"));
398fc5
 			poptPrintUsage(pctx, stdout, 0);
398fc5
+			free(p);
398fc5
+			free(q);
398fc5
 			return CM_SUBMIT_STATUS_UNCONFIGURED;
398fc5
 		}
398fc5
 		if ((sslcert == NULL) || (strlen(sslcert) == 0)) {
398fc5
 			printf(_("No agent credentials (-n) given, but they "
398fc5
 				 "are needed.\n"));
398fc5
 			poptPrintUsage(pctx, stdout, 0);
398fc5
+			free(p);
398fc5
+			free(q);
398fc5
 			return CM_SUBMIT_STATUS_UNCONFIGURED;
398fc5
 		}
398fc5
 		/* Reading profile defaults for this certificate, then applying
398fc5
@@ -778,12 +785,14 @@ main(int argc, const char **argv)
398fc5
 			       lasturl);
398fc5
 		}
398fc5
 		talloc_free(ctx);
398fc5
+		free(p);
398fc5
 		return CM_SUBMIT_STATUS_UNREACHABLE;
398fc5
 	}
398fc5
 	if (results == NULL) {
398fc5
 		printf(_("Internal error: no response to \"%s?%s\".\n"),
398fc5
 		       lasturl, lastparams);
398fc5
 		talloc_free(ctx);
398fc5
+		free(p);
398fc5
 		return CM_SUBMIT_STATUS_REJECTED;
398fc5
 	}
398fc5
 	switch (op) {
398fc5
diff --git a/src/getcert.c b/src/getcert.c
398fc5
index ddb28de2..0d527ab0 100644
398fc5
--- a/src/getcert.c
398fc5
+++ b/src/getcert.c
398fc5
@@ -4042,11 +4042,12 @@ thumbprint(const char *s, SECOidTag tag, int bits)
398fc5
 	}
398fc5
 	u = malloc(length);
398fc5
 	if (u == NULL) {
398fc5
+		free(t);
398fc5
 		goto done;
398fc5
 	}
398fc5
 	length = cm_store_base64_to_bin(t, -1, u, length);
398fc5
+	free(t);
398fc5
 	if (PK11_HashBuf(tag, digest, u, length) == SECSuccess) {
398fc5
-		free(t);
398fc5
 		t = malloc(bits / 4 + howmany(bits, 32));
398fc5
 		if (t != NULL) {
398fc5
 			ret = t;
398fc5
diff --git a/src/store-files.c b/src/store-files.c
398fc5
index b97ba5ff..4e57ae16 100644
398fc5
--- a/src/store-files.c
398fc5
+++ b/src/store-files.c
398fc5
@@ -573,6 +573,7 @@ cm_store_file_read_lines(void *parent, FILE *fp)
398fc5
 			lines = tlines;
398fc5
 		}
398fc5
 	}
398fc5
+	free(buf);
398fc5
 	return lines;
398fc5
 }
398fc5
 
398fc5
diff --git a/src/submit-d.c b/src/submit-d.c
398fc5
index 5a4edb3f..36cc9828 100644
398fc5
--- a/src/submit-d.c
398fc5
+++ b/src/submit-d.c
398fc5
@@ -1204,6 +1204,9 @@ restart:
398fc5
 		} else {
398fc5
 			printf("Error %d.\n", c);
398fc5
 		}
398fc5
+		if (defaults != nodefault) {
398fc5
+			free(defaults);
398fc5
+		}
398fc5
 		return 1;
398fc5
 	}
398fc5
 	result = cm_submit_h_results(hctx, NULL) ?: "";
398fc5
@@ -1365,6 +1368,9 @@ restart:
398fc5
 		/* never reached */
398fc5
 		break;
398fc5
 	}
398fc5
+	if (defaults != nodefault) {
398fc5
+		free(defaults);
398fc5
+	}
398fc5
 	return 0;
398fc5
 }
398fc5
 #endif
398fc5
diff --git a/src/submit-x.c b/src/submit-x.c
398fc5
index abebc610..58d007ef 100644
398fc5
--- a/src/submit-x.c
398fc5
+++ b/src/submit-x.c
398fc5
@@ -45,14 +45,17 @@ get_error_message(krb5_context ctx, krb5_error_code kcode)
398fc5
 {
398fc5
 	const char *ret;
398fc5
 #ifdef HAVE_KRB5_GET_ERROR_MESSAGE
398fc5
-	ret = ctx ? krb5_get_error_message(ctx, kcode) : NULL;
398fc5
-	if (ret == NULL) {
398fc5
-		ret = error_message(kcode);
398fc5
+	if (ctx) {
398fc5
+		const char *msg = krb5_get_error_message(ctx, kcode);
398fc5
+		ret = strdup(msg);
398fc5
+		krb5_free_error_message(ctx, msg);
398fc5
+	} else {
398fc5
+		ret = strdup(error_message(kcode));
398fc5
 	}
398fc5
 #else
398fc5
-	ret = error_message(kcode);
398fc5
+	ret = strdup(error_message(kcode));
398fc5
 #endif
398fc5
-	return strdup(ret);
398fc5
+	return ret;
398fc5
 }
398fc5
 
398fc5
 char *
398fc5
@@ -75,8 +78,6 @@ cm_submit_x_ccache_realm(char **msg)
398fc5
 			ret = get_error_message(ctx, kret));
398fc5
 		if (msg != NULL) {
398fc5
 			*msg = ret;
398fc5
-		} else {
398fc5
-			free(ret);
398fc5
 		}
398fc5
 		return NULL;
398fc5
 	}
398fc5
@@ -86,8 +87,6 @@ cm_submit_x_ccache_realm(char **msg)
398fc5
 			ret = get_error_message(ctx, kret));
398fc5
 		if (msg != NULL) {
398fc5
 			*msg = ret;
398fc5
-		} else {
398fc5
-			free(ret);
398fc5
 		}
398fc5
 		return NULL;
398fc5
 	}
398fc5
@@ -97,8 +96,6 @@ cm_submit_x_ccache_realm(char **msg)
398fc5
 			ret = get_error_message(ctx, kret));
398fc5
 		if (msg != NULL) {
398fc5
 			*msg = ret;
398fc5
-		} else {
398fc5
-			free(ret);
398fc5
 		}
398fc5
 		return NULL;
398fc5
 	}
398fc5
@@ -106,7 +103,7 @@ cm_submit_x_ccache_realm(char **msg)
398fc5
 	if (data == NULL) {
398fc5
 		fprintf(stderr, "Error retrieving principal realm.\n");
398fc5
 		if (msg != NULL) {
398fc5
-			*msg = "Error retrieving principal realm.\n";
398fc5
+			*msg = strdup("Error retrieving principal realm.\n");
398fc5
 		}
398fc5
 		return NULL;
398fc5
 	}
398fc5
@@ -114,7 +111,7 @@ cm_submit_x_ccache_realm(char **msg)
398fc5
 	if (ret == NULL) {
398fc5
 		fprintf(stderr, "Out of memory for principal realm.\n");
398fc5
 		if (msg != NULL) {
398fc5
-			*msg = "Out of memory for principal realm.\n";
398fc5
+			*msg = strdup("Out of memory for principal realm.\n");
398fc5
 		}
398fc5
 		return NULL;
398fc5
 	}
398fc5
@@ -145,8 +142,6 @@ cm_submit_x_make_ccache(const char *ktname, const char *principal, char **msg)
398fc5
 		fprintf(stderr, "Error initializing Kerberos: %s.\n", ret);
398fc5
 		if (msg != NULL) {
398fc5
 			*msg = ret;
398fc5
-		} else {
398fc5
-			free(ret);
398fc5
 		}
398fc5
 		return kret;
398fc5
 	}
398fc5
@@ -160,8 +155,6 @@ cm_submit_x_make_ccache(const char *ktname, const char *principal, char **msg)
398fc5
 			ret = get_error_message(ctx, kret));
398fc5
 		if (msg != NULL) {
398fc5
 			*msg = ret;
398fc5
-		} else {
398fc5
-			free(ret);
398fc5
 		}
398fc5
 		return kret;
398fc5
 	}
398fc5
@@ -173,8 +166,6 @@ cm_submit_x_make_ccache(const char *ktname, const char *principal, char **msg)
398fc5
 				principal, ret = get_error_message(ctx, kret));
398fc5
 			if (msg != NULL) {
398fc5
 				*msg = ret;
398fc5
-			} else {
398fc5
-				free(ret);
398fc5
 			}
398fc5
 			return kret;
398fc5
 		}
398fc5
@@ -186,8 +177,6 @@ cm_submit_x_make_ccache(const char *ktname, const char *principal, char **msg)
398fc5
 				ret = get_error_message(ctx, kret));
398fc5
 			if (msg != NULL) {
398fc5
 				*msg = ret;
398fc5
-			} else {
398fc5
-				free(ret);
398fc5
 			}
398fc5
 			return kret;
398fc5
 		}
398fc5
@@ -209,8 +198,6 @@ cm_submit_x_make_ccache(const char *ktname, const char *principal, char **msg)
398fc5
 			ret = get_error_message(ctx, kret));
398fc5
 		if (msg != NULL) {
398fc5
 			*msg = ret;
398fc5
-		} else {
398fc5
-			free(ret);
398fc5
 		}
398fc5
 		return kret;
398fc5
 	}
398fc5
@@ -229,8 +216,6 @@ cm_submit_x_make_ccache(const char *ktname, const char *principal, char **msg)
398fc5
 			ret = get_error_message(ctx, kret));
398fc5
 		if (msg != NULL) {
398fc5
 			*msg = ret;
398fc5
-		} else {
398fc5
-			free(ret);
398fc5
 		}
398fc5
 		return kret;
398fc5
 	}
398fc5
@@ -245,8 +230,6 @@ cm_submit_x_make_ccache(const char *ktname, const char *principal, char **msg)
398fc5
 			ret = get_error_message(ctx, kret));
398fc5
 		if (msg != NULL) {
398fc5
 			*msg = ret;
398fc5
-		} else {
398fc5
-			free(ret);
398fc5
 		}
398fc5
 		return kret;
398fc5
 	}
398fc5
@@ -257,8 +240,6 @@ cm_submit_x_make_ccache(const char *ktname, const char *principal, char **msg)
398fc5
 			ret = get_error_message(ctx, kret));
398fc5
 		if (msg != NULL) {
398fc5
 			*msg = ret;
398fc5
-		} else {
398fc5
-			free(ret);
398fc5
 		}
398fc5
 		return kret;
398fc5
 	}
398fc5
diff --git a/tests/tools/addcinfo.c b/tests/tools/addcinfo.c
398fc5
index 939005c2..e34612a5 100644
398fc5
--- a/tests/tools/addcinfo.c
398fc5
+++ b/tests/tools/addcinfo.c
398fc5
@@ -63,7 +63,7 @@ content_info_template[] = {
398fc5
 int
398fc5
 main(int argc, char **argv)
398fc5
 {
398fc5
-	unsigned char *buffer = NULL, buf[BUFSIZ];
398fc5
+	unsigned char *buffer = NULL, *newbuffer = NULL, buf[BUFSIZ];
398fc5
 	int i, n = 0;
398fc5
 	unsigned int j;
398fc5
 	SECItem encoded;
398fc5
@@ -73,11 +73,13 @@ main(int argc, char **argv)
398fc5
 	cm_log_set_method(cm_log_stderr);
398fc5
 	cm_log_set_level(3);
398fc5
 	while ((i = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
398fc5
-		buffer = realloc(buffer, n + i);
398fc5
-		if (buffer == NULL) {
398fc5
+		newbuffer = realloc(buffer, n + i);
398fc5
+		if (newbuffer == NULL) {
398fc5
+			free(buffer);
398fc5
 			cm_log(0, "Out of memory.\n");
398fc5
 			return 1;
398fc5
 		}
398fc5
+		buffer = newbuffer;
398fc5
 		memcpy(buffer + n, buf, i);
398fc5
 		n += i;
398fc5
 	}
398fc5
-- 
398fc5
2.21.0
398fc5