2021-12-13 03:03:42

by Xiaoke Wang

[permalink] [raw]
Subject: [PATCH] nfs: nfs4clinet: check the return value of kstrdup()

kstrdup() returns NULL when some internal memory errors happen, it is
better to check the return value of it so to catch the memory error in
time.

Signed-off-by: Xiaoke Wang <[email protected]>
---
fs/nfs/nfs4client.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/fs/nfs/nfs4client.c b/fs/nfs/nfs4client.c
index af57332..89f13e0 100644
--- a/fs/nfs/nfs4client.c
+++ b/fs/nfs/nfs4client.c
@@ -1372,5 +1372,8 @@ int nfs4_update_server(struct nfs_server *server, const char *hostname,
server->nfs_client->cl_hostname = kstrdup(hostname, GFP_KERNEL);
nfs_server_insert_lists(server);

+ if (server->nfs_client->cl_hostname == NULL)
+ return -ENOMEM;
+
return nfs_probe_destination(server);
}
--


2021-12-16 16:39:57

by Anna Schumaker

[permalink] [raw]
Subject: Re: [PATCH] nfs: nfs4clinet: check the return value of kstrdup()

Hi Xiaoke,

On Mon, Dec 13, 2021 at 2:54 AM Xiaoke Wang <[email protected]> wrote:
>
> kstrdup() returns NULL when some internal memory errors happen, it is
> better to check the return value of it so to catch the memory error in
> time.
>
> Signed-off-by: Xiaoke Wang <[email protected]>
> ---
> fs/nfs/nfs4client.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/fs/nfs/nfs4client.c b/fs/nfs/nfs4client.c
> index af57332..89f13e0 100644
> --- a/fs/nfs/nfs4client.c
> +++ b/fs/nfs/nfs4client.c
> @@ -1372,5 +1372,8 @@ int nfs4_update_server(struct nfs_server *server, const char *hostname,
> server->nfs_client->cl_hostname = kstrdup(hostname, GFP_KERNEL);
> nfs_server_insert_lists(server);
>
> + if (server->nfs_client->cl_hostname == NULL)
> + return -ENOMEM;
> +

Checking the return of kstrdup() makes sense, but I think this should
right after the kstrdup() call and still under that if block.

Anna

> return nfs_probe_destination(server);
> }
> --