[PATCH] srcpos.c: Fix dereference after null check

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



When running coverity on dtc source code the following error is reported.

=========================================================================
250 srcpos_string(struct srcpos *pos)
251{
252        const char *fname = "<no-file>";
253        char *pos_str;
254
   1. Condition pos, taking false branch.
   2. var_compare_op: Comparing pos to null implies that pos might be null.
255        if (pos)
256                fname = pos->file->name;
257
258
   CID 1026108 (#1 of 1): Dereference after null check (FORWARD_NULL)3. var_deref_op: Dereferencing null pointer pos.
259        if (pos->first_line != pos->last_line)

=======================================================================

Fixed the srcpos_string() function assuming the pos argument could be NULL.

Fixed the fname assignement checking the name was indeed set in the
provided pos structure.

As srcpos_string() can now return NULL, fixed srcpos_verror() to handle this
situation.

Signed-off-by: Jean-Christophe Dubois <jcd@xxxxxxxxxxxxxxx>
---
 srcpos.c | 44 ++++++++++++++++++++++++++------------------
 1 file changed, 26 insertions(+), 18 deletions(-)

diff --git a/srcpos.c b/srcpos.c
index aa3aad0..cec790c 100644
--- a/srcpos.c
+++ b/srcpos.c
@@ -249,24 +249,28 @@ srcpos_copy(struct srcpos *pos)
 char *
 srcpos_string(struct srcpos *pos)
 {
-	const char *fname = "<no-file>";
-	char *pos_str;
+	char *pos_str = NULL;
 
-	if (pos)
-		fname = pos->file->name;
+	if (pos) {
+		const char *fname = "<no-file>";
 
+		if (pos->file && pos->file->name) {
+			fname = pos->file->name;
+		}
 
-	if (pos->first_line != pos->last_line)
-		xasprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
-			  pos->first_line, pos->first_column,
-			  pos->last_line, pos->last_column);
-	else if (pos->first_column != pos->last_column)
-		xasprintf(&pos_str, "%s:%d.%d-%d", fname,
-			  pos->first_line, pos->first_column,
-			  pos->last_column);
-	else
-		xasprintf(&pos_str, "%s:%d.%d", fname,
-			  pos->first_line, pos->first_column);
+		if (pos->first_line != pos->last_line) {
+			xasprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
+				  pos->first_line, pos->first_column,
+				  pos->last_line, pos->last_column);
+		} else if (pos->first_column != pos->last_column) {
+			xasprintf(&pos_str, "%s:%d.%d-%d", fname,
+				  pos->first_line, pos->first_column,
+				  pos->last_column);
+		} else {
+			xasprintf(&pos_str, "%s:%d.%d", fname,
+				  pos->first_line, pos->first_column);
+		}
+	}
 
 	return pos_str;
 }
@@ -278,11 +282,15 @@ void srcpos_verror(struct srcpos *pos, const char *prefix,
 
 	srcstr = srcpos_string(pos);
 
-	fprintf(stderr, "%s: %s ", prefix, srcstr);
+	if (srcstr) {
+		fprintf(stderr, "%s: %s ", prefix, srcstr);
+		free(srcstr);
+	} else {
+		fprintf(stderr, "%s: <unknown> ", prefix);
+	}
+
 	vfprintf(stderr, fmt, va);
 	fprintf(stderr, "\n");
-
-	free(srcstr);
 }
 
 void srcpos_error(struct srcpos *pos, const char *prefix,
-- 
2.9.3

--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Device Tree]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux