[PATCH v1] fs:dlm:Fix potential null pointer dereference on pointer

From: Wang Ming
Date: Thu Jun 15 2023 - 22:49:17 EST


Before dereferencing the pointer, first judge whether
it is NULL, if it is NULL, there is no need to dereference.
Fix the following coccicheck ERROR:

fs/dlm/lowcomms.c:1082:26-28: ERROR: newcon -> sock is NULL
but dereferenced.

Signed-off-by: Wang Ming <machel@xxxxxxxx>
---
fs/dlm/lowcomms.c | 23 ++++++++++++-----------
1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/fs/dlm/lowcomms.c b/fs/dlm/lowcomms.c
index 3d3802c47..0a2d1b5ab 100644
--- a/fs/dlm/lowcomms.c
+++ b/fs/dlm/lowcomms.c
@@ -1079,7 +1079,18 @@ static int accept_from_sock(void)
sock_set_mark(newsock->sk, mark);

down_write(&newcon->sock_lock);
- if (newcon->sock) {
+ if (newcon->sock == NULL) {
+ /* accept copies the sk after we've saved the callbacks, so we
+ * don't want to save them a second time or comm errors will
+ * result in calling sk_error_report recursively.
+ */
+ add_sock(newsock, newcon);
+
+ /* check if we receved something while adding */
+ lock_sock(newcon->sock->sk);
+ lowcomms_queue_rwork(newcon);
+ release_sock(newcon->sock->sk);
+ } else {
struct connection *othercon = newcon->othercon;

if (!othercon) {
@@ -1110,17 +1121,7 @@ static int accept_from_sock(void)
release_sock(othercon->sock->sk);
up_write(&othercon->sock_lock);
}
- else {
- /* accept copies the sk after we've saved the callbacks, so we
- don't want to save them a second time or comm errors will
- result in calling sk_error_report recursively. */
- add_sock(newsock, newcon);

- /* check if we receved something while adding */
- lock_sock(newcon->sock->sk);
- lowcomms_queue_rwork(newcon);
- release_sock(newcon->sock->sk);
- }
up_write(&newcon->sock_lock);
srcu_read_unlock(&connections_srcu, idx);

--
2.25.1


________________________________
本邮件及其附件内容可能含有机密和/或隐私信息,仅供指定个人或机构使用。若您非发件人指定收件人或其代理人,请勿使用、传播、复制或存储此邮件之任何内容或其附件。如您误收本邮件,请即以回复或电话方式通知发件人,并将原始邮件、附件及其所有复本删除。谢谢。
The contents of this message and any attachments may contain confidential and/or privileged information and are intended exclusively for the addressee(s). If you are not the intended recipient of this message or their agent, please note that any use, dissemination, copying, or storage of this message or its attachments is not allowed. If you receive this message in error, please notify the sender by reply the message or phone and delete this message, any attachments and any copies immediately.
Thank you