Skip to contentSkip to conversation

trace: Fix 'char **' compilation error in simple backend

PATCH Superseded Fam Zheng 2 participants 5 messages Oct 26, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH 1/1 trace: Fix 'char **' compilation error in simple backend

Currently, the generated function body will do "strlen(arg)" but the argument could be 'char **'. Avoid that by exclusding such cases in is_string check.

Reported by patchew's "make docker-test-mingw@fedora".

---
 scripts/tracetool/backend/simple.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/tracetool/backend/simple.py +3 -1
index 9885e83..2538795 100644
@@ -21,7 +21,9 @@ PUBLIC = True
 
 def is_string(arg):
     strtype = ('const char*', 'char*', 'const char *', 'char *')
-    if arg.lstrip().startswith(strtype):
+    non_strtype = ('const char**', 'char**', 'const char **', 'char **')
+    arg_strip = arg.lstrip()
+    if arg_strip.startswith(strtype) and not arg_strip.startswith(non_strtype):
         return True
     else:
         return False
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Fam Zheng

Currently, the generated function body will do "strlen(arg)" but the argument could be 'char **'. Avoid that by exclusding such cases in

s/exclusding/excluding/

Quoted 14 lines

is_string check.

Reported by patchew's "make docker-test-mingw@fedora".

---
 scripts/tracetool/backend/simple.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/tracetool/backend/simple.py +0 -0
index 9885e83..2538795 100644

scripts/tracetool/backend/simple.py PUBLIC = True · line 26

Quoted 7 lines
scripts/tracetool/backend/simple.py +3 -1
@@ -21,7 +21,9 @@ PUBLIC = True
 
 def is_string(arg):
     strtype = ('const char*', 'char*', 'const char *', 'char *')
-    if arg.lstrip().startswith(strtype):
+    non_strtype = ('const char**', 'char**', 'const char **', 'char **')
+    arg_strip = arg.lstrip()
+    if arg_strip.startswith(strtype) and not arg_strip.startswith(non_strtype):

There may be a more compact way to write it, but I'm not enough of a python expert to know offhand what else to suggest (it's not as simple as string concatenation of strtype + '*', since strtype is a tuple rather than a string).

What you have will fail to detect 'const char *const *' as a non-string (possible if we have some argv-like function that takes a constant array of constant strings), but I guess we can worry about that if we actually try to trace something with that signature. In the meantime, what you have solves the immediate failure, so:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#2 lore raw
Quoted 5 lines · Eric Blake
Quoted 2 lines · Fam Zheng

Currently, the generated function body will do "strlen(arg)" but the argument could be 'char **'. Avoid that by exclusding such cases in

s/exclusding/excluding/

Yes, I blame the insomnia last night. @.@

I assume this can be fixed when applying.

Quoted 16 lines
Quoted 16 lines

is_string check.

Reported by patchew's "make docker-test-mingw@fedora".

---
 scripts/tracetool/backend/simple.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
scripts/tracetool/backend/simple.py +0 -0
index 9885e83..2538795 100644
@@ -21,7 +21,9 @@ PUBLIC = True
 
Quoted 11 lines

scripts/tracetool/backend/simple.py PUBLIC = True · line 26

Quoted 6 lines
scripts/tracetool/backend/simple.py +3 -1
 def is_string(arg):
     strtype = ('const char*', 'char*', 'const char *', 'char *')
-    if arg.lstrip().startswith(strtype):
+    non_strtype = ('const char**', 'char**', 'const char **', 'char **')
+    arg_strip = arg.lstrip()
+    if arg_strip.startswith(strtype) and not arg_strip.startswith(non_strtype):

There may be a more compact way to write it, but I'm not enough of a python expert to know offhand what else to suggest (it's not as simple as string concatenation of strtype + '*', since strtype is a tuple rather than a string).

Did you mean

non_strtype = tuple(x + '*' for x in strtype)

?

But personally I'd stick to the flatten version in this specific case for a bit more readability.

Thanks!

Fam

#3 lore raw
Quoted 13 lines · Fam Zheng
Quoted 12 lines · Eric Blake
Quoted 10 lines · Fam Zheng

Currently, the generated function body will do "strlen(arg)" but the argument could be 'char **'. Avoid that by exclusding such cases in

def is_string(arg):

     strtype = ('const char*', 'char*', 'const char *', 'char *')
scripts/tracetool/backend/simple.py +3 -1
-    if arg.lstrip().startswith(strtype):
+    non_strtype = ('const char**', 'char**', 'const char **', 'char **')
+    arg_strip = arg.lstrip()
+    if arg_strip.startswith(strtype) and not arg_strip.startswith(non_strtype):
Quoted 8 lines
Quoted 4 lines

There may be a more compact way to write it, but I'm not enough of a python expert to know offhand what else to suggest (it's not as simple as string concatenation of strtype + '*', since strtype is a tuple rather than a string).

Did you mean

non_strtype = tuple(x + '*' for x in strtype)

Hmm, I guess that would work.

Or, what about a different approach, something like:
if arg_strip.startswith(strtype) and no_multiple_star(arg_strip):
for some sane definition of no_multiple_star() that checks that there is exactly one '*' in a string. In C, I'd check roughly:
p = strchr(str, '*');

  if (p && !strchr(p + 1, '*')) {
    // treat str as string

}
but again, I'm not enough of an expert to pop that out late at night, even if python has an easy one-liner way to express that.

Quoted 2 lines

But personally I'd stick to the flatten version in this specific case for a bit more readability.

Indeed, and that's why I gave R-b as-is, even if it fails when there are multiple 'const' qualifiers in a string with multiple '*' :)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#4 lore raw
Quoted 30 lines · Eric Blake
Quoted 13 lines · Fam Zheng
Quoted 12 lines · Eric Blake
Quoted 10 lines · Fam Zheng

Currently, the generated function body will do "strlen(arg)" but the argument could be 'char **'. Avoid that by exclusding such cases in

def is_string(arg):

     strtype = ('const char*', 'char*', 'const char *', 'char *')
scripts/tracetool/backend/simple.py +3 -1
-    if arg.lstrip().startswith(strtype):
+    non_strtype = ('const char**', 'char**', 'const char **', 'char **')
+    arg_strip = arg.lstrip()
+    if arg_strip.startswith(strtype) and not arg_strip.startswith(non_strtype):
Quoted 8 lines
Quoted 4 lines

There may be a more compact way to write it, but I'm not enough of a python expert to know offhand what else to suggest (it's not as simple as string concatenation of strtype + '*', since strtype is a tuple rather than a string).

Did you mean

non_strtype = tuple(x + '*' for x in strtype)

Hmm, I guess that would work.

Or, what about a different approach, something like:
if arg_strip.startswith(strtype) and no_multiple_star(arg_strip):
for some sane definition of no_multiple_star() that checks that there is exactly one '*' in a string. In C, I'd check roughly:
p = strchr(str, '*');

Quoted 5 lines
  if (p && !strchr(p + 1, '*')) {
    // treat str as string

}
but again, I'm not enough of an expert to pop that out late at night, even if python has an easy one-liner way to express that.

That's indeed a nicer approach:

    if arg_strip.startswith(strtype) and arg_strip.count("*") == 1:

Do you want a respin with your suggested-by? :-)

Fam

Quoted 12 lines
Quoted 4 lines

But personally I'd stick to the flatten version in this specific case for a bit more readability.

Indeed, and that's why I gave R-b as-is, even if it fails when there are multiple 'const' qualifiers in a string with multiple '*' :)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org