Skip to contentSkip to conversation

audio: Convert use of atoi to qemu_strtoi

PATCH In review Nia Alarie 2 participants 4 messages Mar 16, 2018 – Mar 19, 2018

lore
Share & tools
lore

Nia Alarie

author
#0 lore raw

PATCH 1/1 audio: Convert use of atoi to qemu_strtoi

If qemu_strtoi indicates an error, return the default value.

---
 audio/audio.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
audio/audio.c +1 -2
index 6eccdb17ee..d6e91901aa 100644
@@ -335,9 +335,8 @@ static int audio_get_conf_int (const char *key, int defval, int *defaultp)
     char *strval;
 
     strval = getenv (key);
-    if (strval) {
+    if (strval && !qemu_strtoi(strval, NULL, 10, &val)) {
         *defaultp = 0;
-        val = atoi (strval);
         return val;
     }
     else {
Signature · 1 line
2.16.2
#1 lore raw
Quoted 1 line · Nia Alarie

If qemu_strtoi indicates an error, return the default value.

Would it be better to diagnose the error instead of silently returning a default value?

Quoted 22 lines
---
  audio/audio.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
audio/audio.c +1 -2
index 6eccdb17ee..d6e91901aa 100644
@@ -335,9 +335,8 @@ static int audio_get_conf_int (const char *key, int defval, int *defaultp)
      char *strval;
 
      strval = getenv (key);
-    if (strval) {
+    if (strval && !qemu_strtoi(strval, NULL, 10, &val)) {
          *defaultp = 0;
-        val = atoi (strval);
          return val;
      }
      else {
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#2 lore raw
Quoted 27 lines · Eric Blake
Quoted 2 lines · Nia Alarie

If qemu_strtoi indicates an error, return the default value.

Would it be better to diagnose the error instead of silently returning a default value?

Quoted 19 lines
---
  audio/audio.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
audio/audio.c +1 -2
index 6eccdb17ee..d6e91901aa 100644
@@ -335,9 +335,8 @@ static int audio_get_conf_int (const char *key, int
 defval, int *defaultp)
      char *strval;
        strval = getenv (key);
-    if (strval) {
+    if (strval && !qemu_strtoi(strval, NULL, 10, &val)) {
          *defaultp = 0;
-        val = atoi (strval);
          return val;
Quoted 8 lines

audio/audio.c static int audio_get_conf_int (const char *key, int · line 342

Quoted 3 lines
audio/audio.c +0 -0
      }
      else {

--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org

Possibly, while writing these patches I was just going by what was already there. I can see how that would be good.

Should the code provide a warning to the user and continue with the default, or provide the warning and exit? And is it more correct to use dolog() or AUD_log() in this context?

#3 lore raw
Quoted 11 lines · nee
Quoted 8 lines · Eric Blake
Quoted 2 lines · Nia Alarie

If qemu_strtoi indicates an error, return the default value.

Would it be better to diagnose the error instead of silently returning a default value?

Quoted 6 lines

Possibly, while writing these patches I was just going by what was already there. I can see how that would be good.

Should the code provide a warning to the user and continue with the default, or provide the warning and exit? And is it more correct to use dolog() or AUD_log() in this context?

I'll defer to the audio maintainer's opinion on what might be best here.
But my personal preference is that if the only time you can give
invalid input is via a bad command line argument, then print the error and exit immediately (the VM never starts), so that the user can then fix their bad command line and get the value they wanted instead of silently running with a different value all because of a typo that caused us to fail to parse a number.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org