[Qemu-devel] [PATCH v3 08/38] json-lexer: make it safe to call multiple times

Marc-André Lureau posted 38 patches 7 years, 10 months ago
[Qemu-devel] [PATCH v3 08/38] json-lexer: make it safe to call multiple times
Posted by Marc-André Lureau 7 years, 10 months ago
We can easily avoid the burden of checking if the lexer was
initialized prior to calling destroy by the caller, let's do it.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 qobject/json-lexer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c
index 980ba159d6..0eaba43a2c 100644
--- a/qobject/json-lexer.c
+++ b/qobject/json-lexer.c
@@ -386,5 +386,8 @@ int json_lexer_flush(JSONLexer *lexer)
 
 void json_lexer_destroy(JSONLexer *lexer)
 {
-    g_string_free(lexer->token, true);
+    if (lexer->token) {
+        g_string_free(lexer->token, true);
+        lexer->token = NULL;
+    }
 }
-- 
2.17.0.rc1.1.g4c4f2b46a3


Re: [Qemu-devel] [PATCH v3 08/38] json-lexer: make it safe to call multiple times
Posted by Markus Armbruster 7 years, 7 months ago
Marc-André Lureau <marcandre.lureau@redhat.com> writes:

> We can easily avoid the burden of checking if the lexer was
> initialized prior to calling destroy by the caller, let's do it.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  qobject/json-lexer.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c
> index 980ba159d6..0eaba43a2c 100644
> --- a/qobject/json-lexer.c
> +++ b/qobject/json-lexer.c
> @@ -386,5 +386,8 @@ int json_lexer_flush(JSONLexer *lexer)
>  
>  void json_lexer_destroy(JSONLexer *lexer)
>  {
> -    g_string_free(lexer->token, true);
> +    if (lexer->token) {
> +        g_string_free(lexer->token, true);
> +        lexer->token = NULL;
> +    }
>  }

Is this just on general principles[*], or does it enable simplifactions
later in this series?


[*] Write the destructor so that it just works on uninitialized,
all-zeroes objects, on partly initialized objects (constructor failed),
and on completely initialized objects (constructor succeeded).

Re: [Qemu-devel] [PATCH v3 08/38] json-lexer: make it safe to call multiple times
Posted by Marc-André Lureau 7 years, 7 months ago
Hi

On Thu, Jul 5, 2018 at 1:42 PM, Markus Armbruster <armbru@redhat.com> wrote:
> Marc-André Lureau <marcandre.lureau@redhat.com> writes:
>
>> We can easily avoid the burden of checking if the lexer was
>> initialized prior to calling destroy by the caller, let's do it.
>>
>> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
>> ---
>>  qobject/json-lexer.c | 5 ++++-
>>  1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/qobject/json-lexer.c b/qobject/json-lexer.c
>> index 980ba159d6..0eaba43a2c 100644
>> --- a/qobject/json-lexer.c
>> +++ b/qobject/json-lexer.c
>> @@ -386,5 +386,8 @@ int json_lexer_flush(JSONLexer *lexer)
>>
>>  void json_lexer_destroy(JSONLexer *lexer)
>>  {
>> -    g_string_free(lexer->token, true);
>> +    if (lexer->token) {
>> +        g_string_free(lexer->token, true);
>> +        lexer->token = NULL;
>> +    }
>>  }
>
> Is this just on general principles[*], or does it enable simplifactions
> later in this series?
>
>

It allowed further simplification later in the series. I don't know if
this is still necessary after the rebase and other changes, feel free
to delay it.

thanks



-- 
Marc-André Lureau