diff mbox

[v3,08/10] qtest: check the value returned by fopen()

Message ID 1407398512-20780-9-git-send-email-zhang.zhanghailiang@huawei.com
State New
Headers show

Commit Message

Zhanghailiang Aug. 7, 2014, 8:01 a.m. UTC
The function fopen() may fail, so check its return value.

Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
Signed-off-by: Li Liu <john.liuli@huawei.com>
---
 qtest.c                  | 5 +++++
 tests/bios-tables-test.c | 2 ++
 2 files changed, 7 insertions(+)

Comments

Gonglei (Arei) Aug. 7, 2014, 11:14 a.m. UTC | #1
> Subject: [Qemu-devel] [PATCH v3 08/10] qtest: check the value returned by
> fopen()
> 
> The function fopen() may fail, so check its return value.
> 
> Signed-off-by: zhanghailiang <zhang.zhanghailiang@huawei.com>
> Signed-off-by: Li Liu <john.liuli@huawei.com>
> ---
>  qtest.c                  | 5 +++++
>  tests/bios-tables-test.c | 2 ++
>  2 files changed, 7 insertions(+)
> 
> diff --git a/qtest.c b/qtest.c
> index 04a6dc1..ae9b636 100644
> --- a/qtest.c
> +++ b/qtest.c
> @@ -536,6 +536,11 @@ void qtest_init(const char *qtest_chrdev, const char
> *qtest_log, Error **errp)
>      if (qtest_log) {
>          if (strcmp(qtest_log, "none") != 0) {
>              qtest_log_fp = fopen(qtest_log, "w+");
> +            if (qtest_log_fp == NULL) {
> +                error_setg(errp, "Failed to open log file for qtest: \"%s\"",
> +                           qtest_log);
> +                return;
> +            }

Actually I don't think this check is necessary, because the qtest_log_fp will be checked
where it is used.

Best regards,
-Gonglei

>          }
>      } else {
>          qtest_log_fp = stderr;
> diff --git a/tests/bios-tables-test.c b/tests/bios-tables-test.c
> index 045eb27..6a357c0 100644
> --- a/tests/bios-tables-test.c
> +++ b/tests/bios-tables-test.c
> @@ -790,6 +790,8 @@ int main(int argc, char *argv[])
>      const char *arch = qtest_get_arch();
>      FILE *f = fopen(disk, "w");
>      int ret;
> +
> +    g_assert(f != NULL);
>      fwrite(boot_sector, 1, sizeof boot_sector, f);
>      fclose(f);
> 
> --
> 1.7.12.4
> 
>
Zhanghailiang Aug. 7, 2014, 11:33 a.m. UTC | #2
On 2014/8/7 19:14, Gonglei (Arei) wrote:
>> Subject: [Qemu-devel] [PATCH v3 08/10] qtest: check the value returned by
>> fopen()
>>
>> The function fopen() may fail, so check its return value.
>>
>> Signed-off-by: zhanghailiang<zhang.zhanghailiang@huawei.com>
>> Signed-off-by: Li Liu<john.liuli@huawei.com>
>> ---
>>   qtest.c                  | 5 +++++
>>   tests/bios-tables-test.c | 2 ++
>>   2 files changed, 7 insertions(+)
>>
>> diff --git a/qtest.c b/qtest.c
>> index 04a6dc1..ae9b636 100644
>> --- a/qtest.c
>> +++ b/qtest.c
>> @@ -536,6 +536,11 @@ void qtest_init(const char *qtest_chrdev, const char
>> *qtest_log, Error **errp)
>>       if (qtest_log) {
>>           if (strcmp(qtest_log, "none") != 0) {
>>               qtest_log_fp = fopen(qtest_log, "w+");
>> +            if (qtest_log_fp == NULL) {
>> +                error_setg(errp, "Failed to open log file for qtest: \"%s\"",
>> +                           qtest_log);
>> +                return;
>> +            }
>
> Actually I don't think this check is necessary, because the qtest_log_fp will be checked
> where it is used.
>

Hmm, you are right! I will remove the changes!

Thanks,
zhanghailiang

>>           }
>>       } else {
>>           qtest_log_fp = stderr;
>> diff --git a/tests/bios-tables-test.c b/tests/bios-tables-test.c
>> index 045eb27..6a357c0 100644
>> --- a/tests/bios-tables-test.c
>> +++ b/tests/bios-tables-test.c
>> @@ -790,6 +790,8 @@ int main(int argc, char *argv[])
>>       const char *arch = qtest_get_arch();
>>       FILE *f = fopen(disk, "w");
>>       int ret;
>> +
>> +    g_assert(f != NULL);
>>       fwrite(boot_sector, 1, sizeof boot_sector, f);
>>       fclose(f);
>>
>> --
>> 1.7.12.4
>>
diff mbox

Patch

diff --git a/qtest.c b/qtest.c
index 04a6dc1..ae9b636 100644
--- a/qtest.c
+++ b/qtest.c
@@ -536,6 +536,11 @@  void qtest_init(const char *qtest_chrdev, const char *qtest_log, Error **errp)
     if (qtest_log) {
         if (strcmp(qtest_log, "none") != 0) {
             qtest_log_fp = fopen(qtest_log, "w+");
+            if (qtest_log_fp == NULL) {
+                error_setg(errp, "Failed to open log file for qtest: \"%s\"",
+                           qtest_log);
+                return;
+            }
         }
     } else {
         qtest_log_fp = stderr;
diff --git a/tests/bios-tables-test.c b/tests/bios-tables-test.c
index 045eb27..6a357c0 100644
--- a/tests/bios-tables-test.c
+++ b/tests/bios-tables-test.c
@@ -790,6 +790,8 @@  int main(int argc, char *argv[])
     const char *arch = qtest_get_arch();
     FILE *f = fopen(disk, "w");
     int ret;
+
+    g_assert(f != NULL);
     fwrite(boot_sector, 1, sizeof boot_sector, f);
     fclose(f);