Skip to contentSkip to conversation

xfs: Fix Smatch warning in xfs_attr_node_get

PATCH Superseded Allison Collins 2 participants 3 messages Jul 29, 2020 XFS filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: Fix Smatch warning in xfs_attr_node_get

Fix warning: variable dereferenced before check 'state' in xfs_attr_node_get. If xfs_attr_node_hasname fails, it may return a null state. If state is null, do not derefrence it. Go straight to out.

---
 fs/xfs/libxfs/xfs_attr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_attr.c +4 -1
index e5ec9ed..90b7b24 100644
@@ -1409,6 +1409,9 @@ xfs_attr_node_get(
 	 * Search to see if name exists, and get back a pointer to it.
 	 */
 	error = xfs_attr_node_hasname(args, &state);
+	if (!state)
+		goto out;
+
 	if (error != -EEXIST)
 		goto out_release;
 
@@ -1426,7 +1429,7 @@ xfs_attr_node_get(
 		xfs_trans_brelse(args->trans, state->path.blk[i].bp);
 		state->path.blk[i].bp = NULL;
 	}
-
+out:
 	if (state)
 		xfs_da_state_free(state);
 	return error;
Signature · 1 line
2.7.4
#1 lore raw

fs/xfs/libxfs/xfs_attr.c xfs_attr_node_get(

Quoted 24 lines · Allison Collins

Fix warning: variable dereferenced before check 'state' in xfs_attr_node_get. If xfs_attr_node_hasname fails, it may return a null state. If state is null, do not derefrence it. Go straight to out.

---
 fs/xfs/libxfs/xfs_attr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_attr.c +3 -0
index e5ec9ed..90b7b24 100644
@@ -1409,6 +1409,9 @@ xfs_attr_node_get(
 	 * Search to see if name exists, and get back a pointer to it.
 	 */
 	error = xfs_attr_node_hasname(args, &state);
+	if (!state)
+		goto out;
+
 	if (error != -EEXIST)
 		goto out_release;
 
@@ -1426,7 +1429,7 @@ xfs_attr_node_get(

I would've just changed the for loop to:

	for (i = 0; state && i < state->path.active; i++) {

Since that way we'd know that the error-out path always does the right thing wrt any resources that could have been allocated.

--D

Quoted 11 lines
fs/xfs/libxfs/xfs_attr.c +1 -1
 		xfs_trans_brelse(args->trans, state->path.blk[i].bp);
 		state->path.blk[i].bp = NULL;
 	}
-
+out:
 	if (state)
 		xfs_da_state_free(state);
 	return error;
Signature · 1 line
2.7.4
#2 lore raw
Quoted 24 lines · Darrick J. Wong
Quoted 23 lines · Allison Collins

Fix warning: variable dereferenced before check 'state' in xfs_attr_node_get. If xfs_attr_node_hasname fails, it may return a null state. If state is null, do not derefrence it. Go straight to out.

---
  fs/xfs/libxfs/xfs_attr.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_attr.c +3 -0
index e5ec9ed..90b7b24 100644
@@ -1409,6 +1409,9 @@ xfs_attr_node_get(
  	 * Search to see if name exists, and get back a pointer to it.
  	 */
  	error = xfs_attr_node_hasname(args, &state);
+	if (!state)
+		goto out;
+
  	if (error != -EEXIST)
  		goto out_release;
 
Quoted 9 lines

fs/xfs/libxfs/xfs_attr.c xfs_attr_node_get(

Quoted 1 line
fs/xfs/libxfs/xfs_attr.c +0 -0
@@ -1426,7 +1429,7 @@ xfs_attr_node_get(

I would've just changed the for loop to:

	for (i = 0; state && i < state->path.active; i++) {

Since that way we'd know that the error-out path always does the right thing wrt any resources that could have been allocated.

Sure, will update

Allison

Quoted 13 lines

--D

Quoted 11 lines
fs/xfs/libxfs/xfs_attr.c +1 -1
  		xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  		state->path.blk[i].bp = NULL;
  	}
-
+out:
  	if (state)
  		xfs_da_state_free(state);
  	return error;
Signature · 1 line
2.7.4