diff --git a/src/tools.c b/src/tools.c
index dd0caee9becf8904a1f3df411014801360fab5ab..1ed091fae4e603f83024969efd85dcb97cd4b103 100644
--- a/src/tools.c
+++ b/src/tools.c
@@ -256,20 +256,24 @@ char *generate_duid(bool sysnchronise, int number)
 int run_cmd(const char *cmd, char *output, size_t out_len)
 {
 	int ret = -1;
-	FILE *pp; // program pointer
+	FILE *pp = NULL; // program pointer
 
 	if (cmd == NULL) // null command to run, silently ignore
 		return 0;
 
-	// init the buffer
-	memset(output, 0, out_len);
-
 	pp = popen(cmd, "r"); // flawfinder: ignore
+	if (pp == NULL) {
+		fprintf(stderr, "Failed to run [%s]", cmd);
+		return -1;
+	}
 
 	if (output == NULL || out_len == 0) {
+		ret = pclose(pp);
 		return ret;
 	}
 
+	// init the buffer
+	memset(output, 0, out_len);
 	if (pp != NULL) {
 		char line[512] = {0};