diff mbox series

selftests: vm: Fix resource leak when return error

Message ID 20220615093629.1330809-1-dingxiang@cmss.chinamobile.com (mailing list archive)
State Accepted
Commit 3084a4ec7f9bb1ec90036cfd01b1abadc5dd4fb2
Headers show
Series selftests: vm: Fix resource leak when return error | expand

Commit Message

Ding Xiang June 15, 2022, 9:36 a.m. UTC
When return on an error path, file handle need to be closed
to prevent resource leak

Signed-off-by: Ding Xiang <dingxiang@cmss.chinamobile.com>
---
 tools/testing/selftests/vm/ksm_tests.c | 2 ++
 1 file changed, 2 insertions(+)

Comments

Shuah Khan June 16, 2022, 8:11 p.m. UTC | #1
On 6/15/22 3:36 AM, Ding Xiang wrote:
> When return on an error path, file handle need to be closed
> to prevent resource leak
> 
> Signed-off-by: Ding Xiang <dingxiang@cmss.chinamobile.com>
> ---
>   tools/testing/selftests/vm/ksm_tests.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c
> index 2fcf24312da8..f5e4e0bbd081 100644
> --- a/tools/testing/selftests/vm/ksm_tests.c
> +++ b/tools/testing/selftests/vm/ksm_tests.c
> @@ -54,6 +54,7 @@ static int ksm_write_sysfs(const char *file_path, unsigned long val)
>   	}
>   	if (fprintf(f, "%lu", val) < 0) {
>   		perror("fprintf");
> +		fclose(f);
>   		return 1;
>   	}
>   	fclose(f);
> @@ -72,6 +73,7 @@ static int ksm_read_sysfs(const char *file_path, unsigned long *val)
>   	}
>   	if (fscanf(f, "%lu", val) != 1) {
>   		perror("fscanf");
> +		fclose(f);
>   		return 1;
>   	}
>   	fclose(f);
> 

Looks good to me.

Reviewed-by: Shuah Khan <skhan@linuxfoundation.org>

thanks,
-- Shuah
Shuah Khan June 16, 2022, 8:19 p.m. UTC | #2
On 6/16/22 2:11 PM, Shuah Khan wrote:
> On 6/15/22 3:36 AM, Ding Xiang wrote:
>> When return on an error path, file handle need to be closed
>> to prevent resource leak
>>
>> Signed-off-by: Ding Xiang <dingxiang@cmss.chinamobile.com>
>> ---
>>   tools/testing/selftests/vm/ksm_tests.c | 2 ++
>>   1 file changed, 2 insertions(+)
>>
>> diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c
>> index 2fcf24312da8..f5e4e0bbd081 100644
>> --- a/tools/testing/selftests/vm/ksm_tests.c
>> +++ b/tools/testing/selftests/vm/ksm_tests.c
>> @@ -54,6 +54,7 @@ static int ksm_write_sysfs(const char *file_path, unsigned long val)
>>       }
>>       if (fprintf(f, "%lu", val) < 0) {
>>           perror("fprintf");
>> +        fclose(f);
>>           return 1;
>>       }
>>       fclose(f);
>> @@ -72,6 +73,7 @@ static int ksm_read_sysfs(const char *file_path, unsigned long *val)
>>       }
>>       if (fscanf(f, "%lu", val) != 1) {
>>           perror("fscanf");
>> +        fclose(f);
>>           return 1;
>>       }
>>       fclose(f);
>>
> 
> Looks good to me.
> 
> Reviewed-by: Shuah Khan <skhan@linuxfoundation.org>
> 

I applied this to linux-kselftest fixes for next rc.

thanks,
-- Shuah
diff mbox series

Patch

diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c
index 2fcf24312da8..f5e4e0bbd081 100644
--- a/tools/testing/selftests/vm/ksm_tests.c
+++ b/tools/testing/selftests/vm/ksm_tests.c
@@ -54,6 +54,7 @@  static int ksm_write_sysfs(const char *file_path, unsigned long val)
 	}
 	if (fprintf(f, "%lu", val) < 0) {
 		perror("fprintf");
+		fclose(f);
 		return 1;
 	}
 	fclose(f);
@@ -72,6 +73,7 @@  static int ksm_read_sysfs(const char *file_path, unsigned long *val)
 	}
 	if (fscanf(f, "%lu", val) != 1) {
 		perror("fscanf");
+		fclose(f);
 		return 1;
 	}
 	fclose(f);