migration/options: Fix leaks in StrOrNull accessors
PATCH 1/1 migration/options: Fix leaks in StrOrNull accessors
Fix a couple of leaks detected by Coverity. Both are currently harmless because the visitor in the setter can never fail and the whole of the getter is unused, it's only purpose at the moment is to provide a complete implementation of the StrOrNull property.
---
CI run: https://gitlab.com/farosas/qemu/-/pipelines/2280325023
--- migration/options.c | 2 ++ 1 file changed, 2 insertions(+)
migration/options.c +2 -0
@@ -225,6 +225,7 @@ static void get_StrOrNull(Object *obj, Visitor *v, const char *name, str_or_null =g_new0 (StrOrNull,1 ); str_or_null->type =QTYPE_QSTRING ; str_or_null->u.s =g_strdup ("" ); + *ptr = str_or_null; }else {/* the setter doesn't allow QNULL */ assert (str_or_null->type !=QTYPE_QNULL );
@@ -245,6 +246,7 @@ static void set_StrOrNull(Object *obj, Visitor *v, const char *name,*/ str_or_null->type =QTYPE_QSTRING ;if (!visit_type_str (v, name, &str_or_null->u.s, errp)) { +qapi_free_StrOrNull (str_or_null);return ; }
Signature · 1 line
2.51.0
Quoted 3 lines · Fabiano Rosas
Fix a couple of leaks detected by Coverity. Both are currently harmless because the visitor in the setter can never fail and the whole of the getter is unused, it's only purpose at the moment is to
* unused, it's -> unused. Its
Quoted 25 lines
provide a complete implementation of the StrOrNull property.
Fixes CID 1643919Fixes CID 1643920Reported-by Peter Maydell <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>---
CI run: https://gitlab.com/farosas/qemu/-/pipelines/2280325023--- migration/options.c | 2 ++ 1 file changed, 2 insertions(+)migration/options.c +1
-0@@ -225,6 +225,7 @@ static void get_StrOrNull(Object *obj, Visitor *v, const char *name, str_or_null =g_new0 (StrOrNull,1 ); str_or_null->type =QTYPE_QSTRING ; str_or_null->u.s =g_strdup ("" ); + *ptr = str_or_null; }else {/* the setter doesn't allow QNULL */ assert (str_or_null->type !=QTYPE_QNULL );
migration/options.c static void set_StrOrNull(Object *obj, Visitor *v, const char *name, · line 247
Quoted 2 lines
migration/options.c +0
-0@@ -245,6 +246,7 @@ static void set_StrOrNull(Object *obj, Visitor *v, const char *name,*/ str_or_null->type =QTYPE_QSTRING ;
* Do we need to add: str_or_null->u.s = g_strdup(""); here?
migration/options.c static void set_StrOrNull(Object *obj, Visitor *v, const char *name,
Quoted 6 lines
migration/options.c +1
-1if (!visit_type_str (v, name, &str_or_null->u.s, errp)) { +qapi_free_StrOrNull (str_or_null);return ; } --
* visit_type_str() failure is handled in set_StrOrNull, but not in get_StrOrNull? Do we need to add qapi_free_StrOrNull(str_or_null) in get_StrOrNull()?
* Otherwise it looks okay.
Thank you.
---
- Prasad
Quoted 7 lines · Prasad Pandit
Quoted 3 lines · Fabiano Rosas
Fix a couple of leaks detected by Coverity. Both are currently harmless because the visitor in the setter can never fail and the whole of the getter is unused, it's only purpose at the moment is to
* unused, it's -> unused. Its
Thanks
Quoted 21 lines
Quoted 21 lines
provide a complete implementation of the StrOrNull property.
Fixes CID 1643919Fixes CID 1643920Reported-by Peter Maydell <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>---
CI run: https://gitlab.com/farosas/qemu/-/pipelines/2280325023--- migration/options.c | 2 ++ 1 file changed, 2 insertions(+)migration/options.c +1
-0@@ -225,6 +225,7 @@ static void get_StrOrNull(Object *obj, Visitor *v, const char *name, str_or_null =g_new0 (StrOrNull,1 ); str_or_null->type =QTYPE_QSTRING ; str_or_null->u.s =g_strdup ("" ); + *ptr = str_or_null;
Quoted 9 lines
migration/options.c static void set_StrOrNull(Object *obj, Visitor *v, const char *name, · line 247
Quoted 6 lines
migration/options.c +0
-0}else {/* the setter doesn't allow QNULL */ assert (str_or_null->type !=QTYPE_QNULL );@@ -245,6 +246,7 @@ static void set_StrOrNull(Object *obj, Visitor *v, const char *name,*/ str_or_null->type =QTYPE_QSTRING ;* Do we need to add:
str_or_null->u.s=g_strdup(""); here?
It would leak, the visitor will allocate new memory for it below.
Quoted 11 lines
migration/options.c static void set_StrOrNull(Object *obj, Visitor *v, const char *name,
Quoted 6 lines
migration/options.c +1
-1if (!visit_type_str (v, name, &str_or_null->u.s, errp)) { +qapi_free_StrOrNull (str_or_null);return ; } --*
visit_type_str()failure is handled inset_StrOrNull, but not inget_StrOrNull? Do we need to addqapi_free_StrOrNull(str_or_null)inget_StrOrNull()?
I'd rather leave to the caller. The errp will be set, I think it's enough. Looking at the other instances of visit_type_str in qdev, they all simply return without any handling. I think in practice it's unlikely that the string visitors will ever set errp because they are just a g_strdup() usually.
Quoted 6 lines
* Otherwise it looks okay.
Reviewed-by Prasad Pandit <[email protected]>Thank you.
---
- Prasad
Quoted 18 lines · Fabiano Rosas
Quoted 9 lines
Quoted 9 lines
migration/options.c +1
-0@@ -225,6 +225,7 @@ static void get_StrOrNull(Object *obj, Visitor *v, const char *name, str_or_null =g_new0 (StrOrNull,1 ); str_or_null->type =QTYPE_QSTRING ; str_or_null->u.s = g_strdup(""); <== [1] + *ptr = str_or_null;Quoted 8 lines
migration/options.c static void set_StrOrNull(Object *obj, Visitor *v, const char *name, · line 247
Quoted 6 lines
migration/options.c +0
-0}else {/* the setter doesn't allow QNULL */ assert (str_or_null->type !=QTYPE_QNULL );@@ -245,6 +246,7 @@ static void set_StrOrNull(Object *obj, Visitor *v, const char *name,*/ str_or_null->type =QTYPE_QSTRING ;* Do we need to add:
str_or_null->u.s=g_strdup(""); here?
Quoted 1 line
It would leak, the visitor will allocate new memory for it below.
* Then do we need to remove it from get_StrOrNull()? <== [1] above.
Quoted 5 lines
I'd rather leave to the caller. The errp will be set, I think it's enough. Looking at the other instances of
visit_type_strin qdev, they all simply return without any handling. I think in practice it's unlikely that the string visitors will ever set errp because they are just ag_strdup()usually.
* Hmmn, okay. Coverity reported a leak for it in set_StrOrNull(), not other places?
Thank you.
---
- Prasad
Quoted 19 lines · Prasad Pandit
Quoted 18 lines · Fabiano Rosas
Quoted 17 lines
Quoted 13 lines
migration/options.c +1
-0@@ -225,6 +225,7 @@ static void get_StrOrNull(Object *obj, Visitor *v, const char *name, str_or_null =g_new0 (StrOrNull,1 ); str_or_null->type =QTYPE_QSTRING ; str_or_null->u.s = g_strdup(""); <== [1] + *ptr = str_or_null; }else {/* the setter doesn't allow QNULL */ assert (str_or_null->type !=QTYPE_QNULL );migration/options.c static void set_StrOrNull(Object *obj, Visitor *v, const char *name, · line 247
Quoted 2 lines
migration/options.c +0
-0@@ -245,6 +246,7 @@ static void set_StrOrNull(Object *obj, Visitor *v, const char *name,*/ str_or_null->type =QTYPE_QSTRING ;* Do we need to add:
str_or_null->u.s=g_strdup(""); here?
Quoted 4 lines
Quoted 1 line
It would leak, the visitor will allocate new memory for it below.
* Then do we need to remove it from
get_StrOrNull()? <== [1] above.
The visitor behaves differently depending on the direction (input/output), the getter is providing a new object with a default empty string.
Quoted 9 lines
Quoted 5 lines
I'd rather leave to the caller. The errp will be set, I think it's enough. Looking at the other instances of
visit_type_strin qdev, they all simply return without any handling. I think in practice it's unlikely that the string visitors will ever set errp because they are just ag_strdup()usually.* Hmmn, okay. Coverity reported a leak for it in
set_StrOrNull(), not other places?
Well, the setter is slightly different because this property is a wrapper that contains a string, so the string visitor in the setter can (in theory) fail and that would indeed leak (the outer object's memory). But the other properties are returning the string directly, without any extra memory allocation so, as in this getter, they simply return on (the theoretical) failure.
IOW, there is nothing to leak in the other setters and the other getters, on failure leave the caller to deal with the memory that was already allocated.
Now, I just double-checked and what I said in the previous email is not entirely accurate:
"I think in practice it's unlikely that the string visitors will ever set errp because they are just a g_strdup() usually." -- me
the input visitor can actually set errp, but it does so before allocating the string.
Quoted 3 lines
Thank you.
---
- Prasad
Quoted 20 lines · Fabiano Rosas
Fix a couple of leaks detected by Coverity. Both are currently harmless because the visitor in the setter can never fail and the whole of the getter is unused, it's only purpose at the moment is to provide a complete implementation of the StrOrNull property.
Fixes CID 1643919Fixes CID 1643920Reported-by Peter Maydell <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>---
CI run: https://gitlab.com/farosas/qemu/-/pipelines/2280325023--- migration/options.c | 2 ++ 1 file changed, 2 insertions(+)migration/options.c +0
-0
migration/options.c static void get_StrOrNull(Object *obj, Visitor *v, const char *name, · line 228
Quoted 4 lines
migration/options.c +1
-0@@ -225,6 +225,7 @@ static void get_StrOrNull(Object *obj, Visitor *v, const char *name, str_or_null =g_new0 (StrOrNull,1 ); str_or_null->type =QTYPE_QSTRING ; str_or_null->u.s =g_strdup ("" ); + *ptr = str_or_null;
Yep, fixes the leak.
However does it then mean a get() can internally update this Property? It used to be NULL, now it's QTYPE_STRING "".
It may make slightly more sense to me to have zero side effect on a get() call. Say, keeping *ptr==NULL after get() returns (as before), while we can use a temp string ("") for the visitor.
I randomly checked another get() provider, e.g. get_drive() does that. Should we follow?
migration/options.c static void set_StrOrNull(Object *obj, Visitor *v, const char *name, · line 249
Quoted 8 lines
migration/options.c +1
-0}else {/* the setter doesn't allow QNULL */ assert (str_or_null->type !=QTYPE_QNULL );@@ -245,6 +246,7 @@ static void set_StrOrNull(Object *obj, Visitor *v, const char *name,*/ str_or_null->type =QTYPE_QSTRING ;if (!visit_type_str (v, name, &str_or_null->u.s, errp)) { +qapi_free_StrOrNull (str_or_null);
This looks correct. Or, simpler way is we only do g_new0() after the visit_type_str() successfully returned (and pass a temp char* into it).
Quoted 6 lines
migration/options.c +0
-0return ; }Signature · 1 line
2.51.0
Signature · 1 line
Peter Xu
Quoted 31 lines · Peter Xu
Quoted 20 lines · Fabiano Rosas
Fix a couple of leaks detected by Coverity. Both are currently harmless because the visitor in the setter can never fail and the whole of the getter is unused, it's only purpose at the moment is to provide a complete implementation of the StrOrNull property.
Fixes CID 1643919Fixes CID 1643920Reported-by Peter Maydell <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>---
CI run: https://gitlab.com/farosas/qemu/-/pipelines/2280325023--- migration/options.c | 2 ++ 1 file changed, 2 insertions(+)migration/options.c +0
-0migration/options.c static void get_StrOrNull(Object *obj, Visitor *v, const char *name, · line 228
Quoted 4 lines
migration/options.c +1
-0@@ -225,6 +225,7 @@ static void get_StrOrNull(Object *obj, Visitor *v, const char *name, str_or_null =g_new0 (StrOrNull,1 ); str_or_null->type =QTYPE_QSTRING ; str_or_null->u.s =g_strdup ("" ); + *ptr = str_or_null;Yep, fixes the leak.
However does it then mean a
get()can internally update this Property? It used to be NULL, now it'sQTYPE_STRING"".
Quoted 7 lines
It may make slightly more sense to me to have zero side effect on a
get()call. Say, keeping *ptr==NULL afterget()returns (as before), while we can use a temp string ("") for the visitor.I randomly checked another
get()provider, e.g.get_drive()does that. Should we follow?
Hmm, I'm actually inclined to assert. This is code for the very specific purpose of allowing compatibility of -global tls-* options which are properties of the s->parameters object.
The only way the property could be NULL in the getter is if the setter has failed. Is there a way we could make the setter fail without it being a programming error? I don't see it.
- the default for the property is an empty string.
- setting via set-parameters bypasses the qdev code entirely.
- device_add is not reachable because of dc->user_creatable = false.
- object_set_propv and qom_set use a string visitor, so no type mismatch to trigger the qobject_to(QString, qobj) cast.
Quoted 12 lines
migration/options.c static void set_StrOrNull(Object *obj, Visitor *v, const char *name, · line 249
Quoted 8 lines
migration/options.c +1
-0}else {/* the setter doesn't allow QNULL */ assert (str_or_null->type !=QTYPE_QNULL );@@ -245,6 +246,7 @@ static void set_StrOrNull(Object *obj, Visitor *v, const char *name,*/ str_or_null->type =QTYPE_QSTRING ;if (!visit_type_str (v, name, &str_or_null->u.s, errp)) { +qapi_free_StrOrNull (str_or_null);This looks correct. Or, simpler way is we only do
g_new0()after thevisit_type_str()successfully returned (and pass a temp char* into it).
I'll update it.
Quoted 6 lines
Quoted 6 lines
migration/options.c +0
-0return ; }Signature · 1 line
2.51.0
Quoted 49 lines · Fabiano Rosas
Quoted 31 lines · Peter Xu
Quoted 20 lines · Fabiano Rosas
Fix a couple of leaks detected by Coverity. Both are currently harmless because the visitor in the setter can never fail and the whole of the getter is unused, it's only purpose at the moment is to provide a complete implementation of the StrOrNull property.
Fixes CID 1643919Fixes CID 1643920Reported-by Peter Maydell <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>---
CI run: https://gitlab.com/farosas/qemu/-/pipelines/2280325023--- migration/options.c | 2 ++ 1 file changed, 2 insertions(+)migration/options.c +0
-0migration/options.c static void get_StrOrNull(Object *obj, Visitor *v, const char *name, · line 228
Quoted 4 lines
migration/options.c +1
-0@@ -225,6 +225,7 @@ static void get_StrOrNull(Object *obj, Visitor *v, const char *name, str_or_null =g_new0 (StrOrNull,1 ); str_or_null->type =QTYPE_QSTRING ; str_or_null->u.s =g_strdup ("" ); + *ptr = str_or_null;Yep, fixes the leak.
However does it then mean a
get()can internally update this Property? It used to be NULL, now it'sQTYPE_STRING"".Quoted 7 lines
It may make slightly more sense to me to have zero side effect on a
get()call. Say, keeping *ptr==NULL afterget()returns (as before), while we can use a temp string ("") for the visitor.I randomly checked another
get()provider, e.g.get_drive()does that. Should we follow?Hmm, I'm actually inclined to assert. This is code for the very specific purpose of allowing compatibility of -global tls-* options which are properties of the
s->parametersobject.The only way the property could be NULL in the getter is if the setter has failed. Is there a way we could make the setter fail without it being a programming error? I don't see it.
Quoted 8 lines
- the default for the property is an empty string.
- setting via set-parameters bypasses the qdev code entirely.
-
device_addis not reachable because ofdc->user_creatable= false.-
object_set_propvandqom_setuse a string visitor, so no type mismatch to trigger theqobject_to(QString, qobj)cast.
I agree, assert would even be better. Then, maybe let's add a quick comment for it? Something like:
/*
* Assert for both str_or_null be present and not QNULL.
* Guaranteed because ...
*/
assert(str_or_null && str_or_null->type != QTYPE_QNULL);Signature · 1 line
Peter Xu