Skip to contentSkip to conversation

usb: trivial fixes

PATCH Superseded Peter Xu 4 participants 8 messages Mar 9, 2016 – Mar 10, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

cover usb: trivial fixes

Both for usb_mtp_add_str: fix unbounded stack for it, also cleanup one variable.

Peter Xu (2):
usb: fix unbound stack usage for usb_mtp_add_str
usb: trivial cleanup for usb_mtp_add_str

 hw/usb/dev-mtp.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.4.3
author
#1 lore raw

PATCH 1/2 usb: fix unbound stack usage for usb_mtp_add_str

Use heap instead of stack.

---
 hw/usb/dev-mtp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/usb/dev-mtp.c +3 -1
index 7391783..cf63fd0 100644
@@ -718,7 +718,7 @@ static void usb_mtp_add_wstr(MTPData *data, const wchar_t *str)
 static void usb_mtp_add_str(MTPData *data, const char *str)
 {
     uint32_t len = strlen(str)+1;
-    wchar_t wstr[len];
+    wchar_t *wstr = g_malloc(sizeof(wchar_t) * len);
     size_t ret;
 
     ret = mbstowcs(wstr, str, len);
@@ -727,6 +727,8 @@ static void usb_mtp_add_str(MTPData *data, const char *str)
     } else {
         usb_mtp_add_wstr(data, wstr);
     }
+
+    g_free(wstr);
 }
 
 static void usb_mtp_add_time(MTPData *data, time_t time)
Signature · 1 line
2.4.3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] usb: fix unbound stack usage for usb_mtp_add_str

Quoted 12 lines · Peter Xu

Use heap instead of stack.

---
 hw/usb/dev-mtp.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/usb/dev-mtp.c +0 -0
index 7391783..cf63fd0 100644

hw/usb/dev-mtp.c static void usb_mtp_add_wstr(MTPData *data, const wchar_t *str) · line 721

Quoted 5 lines
hw/usb/dev-mtp.c +1 -1
@@ -718,7 +718,7 @@ static void usb_mtp_add_wstr(MTPData *data, const wchar_t *str)
 static void usb_mtp_add_str(MTPData *data, const char *str)
 {
     uint32_t len = strlen(str)+1;
-    wchar_t wstr[len];
+    wchar_t *wstr = g_malloc(sizeof(wchar_t) * len);

I think it is better to use g_new() in this case.

Fam

Quoted 16 lines
hw/usb/dev-mtp.c +2 -0
     size_t ret;
 
     ret = mbstowcs(wstr, str, len);
@@ -727,6 +727,8 @@ static void usb_mtp_add_str(MTPData *data, const char *str)
     } else {
         usb_mtp_add_wstr(data, wstr);
     }
+
+    g_free(wstr);
 }
 
 static void usb_mtp_add_time(MTPData *data, time_t time)
Signature · 1 line
2.4.3
author
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] usb: fix unbound stack usage for usb_mtp_add_str

Quoted 4 lines · Fam Zheng
Quoted 1 line · Peter Xu

+ wchar_t *wstr = g_malloc(sizeof(wchar_t) * len);

I think it is better to use g_new() in this case.

Okay. Will re-send this one.

Thanks.
Peter

author
#4 lore raw

PATCH 2/2 usb: trivial cleanup for usb_mtp_add_str

In reply to Peter Xu

Remove useless var "ret".

---
 hw/usb/dev-mtp.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hw/usb/dev-mtp.c +1 -3
index cf63fd0..38cc4fc 100644
@@ -719,10 +719,8 @@ static void usb_mtp_add_str(MTPData *data, const char *str)
 {
     uint32_t len = strlen(str)+1;
     wchar_t *wstr = g_malloc(sizeof(wchar_t) * len);
-    size_t ret;
 
-    ret = mbstowcs(wstr, str, len);
-    if (ret == -1) {
+    if (mbstowcs(wstr, str, len) == -1) {
         usb_mtp_add_wstr(data, L"Oops");
     } else {
         usb_mtp_add_wstr(data, wstr);
Signature · 1 line
2.4.3
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] usb: trivial cleanup for usb_mtp_add_str

Quoted 27 lines · Peter Xu

Remove useless var "ret".

---
 hw/usb/dev-mtp.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hw/usb/dev-mtp.c +1 -3
index cf63fd0..38cc4fc 100644
@@ -719,10 +719,8 @@ static void usb_mtp_add_str(MTPData *data, const char *str)
 {
     uint32_t len = strlen(str)+1;
     wchar_t *wstr = g_malloc(sizeof(wchar_t) * len);
-    size_t ret;
 
-    ret = mbstowcs(wstr, str, len);
-    if (ret == -1) {
+    if (mbstowcs(wstr, str, len) == -1) {
         usb_mtp_add_wstr(data, L"Oops");
     } else {
         usb_mtp_add_wstr(data, wstr);
Signature · 1 line
2.4.3

The old way has no problem, no need to clean up, IMO.

Fam

#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] usb: trivial cleanup for usb_mtp_add_str

Quoted 18 lines · Fam Zheng
Quoted 17 lines · Peter Xu

Remove useless var "ret".

---
 hw/usb/dev-mtp.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hw/usb/dev-mtp.c +0 -1
index cf63fd0..38cc4fc 100644
@@ -719,10 +719,8 @@ static void usb_mtp_add_str(MTPData *data, const char *str)
 {
     uint32_t len = strlen(str)+1;
     wchar_t *wstr = g_malloc(sizeof(wchar_t) * len);
-    size_t ret;
 
Quoted 12 lines
Quoted 10 lines
hw/usb/dev-mtp.c +1 -2
-    ret = mbstowcs(wstr, str, len);
-    if (ret == -1) {
+    if (mbstowcs(wstr, str, len) == -1) {
         usb_mtp_add_wstr(data, L"Oops");
     } else {
         usb_mtp_add_wstr(data, wstr);
Signature · 1 line
2.4.3

The old way has no problem, no need to clean up, IMO.

It's a very small readability improvement, but it's an improvement. If I was the maintainer, I'd take it.

#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] usb: trivial cleanup for usb_mtp_add_str

Quoted 33 lines · Markus Armbruster
Quoted 18 lines · Fam Zheng
Quoted 17 lines · Peter Xu

Remove useless var "ret".

---
 hw/usb/dev-mtp.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hw/usb/dev-mtp.c +0 -1
index cf63fd0..38cc4fc 100644
@@ -719,10 +719,8 @@ static void usb_mtp_add_str(MTPData *data, const char *str)
 {
     uint32_t len = strlen(str)+1;
     wchar_t *wstr = g_malloc(sizeof(wchar_t) * len);
-    size_t ret;
 
Quoted 12 lines
Quoted 10 lines
hw/usb/dev-mtp.c +1 -2
-    ret = mbstowcs(wstr, str, len);
-    if (ret == -1) {
+    if (mbstowcs(wstr, str, len) == -1) {
         usb_mtp_add_wstr(data, L"Oops");
     } else {
         usb_mtp_add_wstr(data, wstr);
Signature · 1 line
2.4.3

The old way has no problem, no need to clean up, IMO.

Quoted 2 lines

It's a very small readability improvement, but it's an improvement. If I was the maintainer, I'd take it.

Matter of taste, I find the version with ret more readable (not surprising given I wrote that code ;)

It's easier to see what the "if (...)" condition is because the line is shorter.

cheers,
Gerd