usb: trivial fixes
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
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
@@ -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
PATCH 2/2 usb: trivial cleanup for usb_mtp_add_str
In reply to Peter XuRemove useless var "ret".
--- hw/usb/dev-mtp.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
hw/usb/dev-mtp.c +1 -3
@@ -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
Re: [Qemu-devel] [PATCH 1/2] usb: fix unbound stack usage for usb_mtp_add_str
In reply to Peter XuQuoted 12 lines · Peter Xu
Use heap instead of stack.
Signed-off-by Peter Xu <[email protected]>--- hw/usb/dev-mtp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)hw/usb/dev-mtp.c +0
-0
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
-0size_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
Re: [Qemu-devel] [PATCH 2/2] usb: trivial cleanup for usb_mtp_add_str
In reply to Peter XuQuoted 27 lines · Peter Xu
Remove useless var "ret".
Signed-off-by Peter Xu <[email protected]>--- hw/usb/dev-mtp.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)hw/usb/dev-mtp.c +1
-3@@ -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
Re: [Qemu-devel] [PATCH 1/2] usb: fix unbound stack usage for usb_mtp_add_str
In reply to Fam ZhengQuoted 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
Re: [Qemu-devel] [PATCH 2/2] usb: trivial cleanup for usb_mtp_add_str
In reply to Fam ZhengQuoted 18 lines · Fam Zheng
Quoted 17 lines · Peter Xu
Remove useless var "ret".
Signed-off-by Peter Xu <[email protected]>--- hw/usb/dev-mtp.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)hw/usb/dev-mtp.c +0
-1@@ -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.3The 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.
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".
Signed-off-by Peter Xu <[email protected]>--- hw/usb/dev-mtp.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)hw/usb/dev-mtp.c +0
-1@@ -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.3The 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