diff mbox

[5/5] qga: check utf8-to-utf16 conversion

Message ID 1455727675-20625-6-git-send-email-marcandre.lureau@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Marc-André Lureau Feb. 17, 2016, 4:47 p.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

UTF8 to UTF16 conversion can fail for genuine reasons, let's check errors.

Reported-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 qga/commands-win32.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

Comments

Michael Roth Feb. 24, 2016, 3:38 p.m. UTC | #1
Quoting marcandre.lureau@redhat.com (2016-02-17 10:47:55)
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> UTF8 to UTF16 conversion can fail for genuine reasons, let's check errors.
> 
> Reported-by: Laszlo Ersek <lersek@redhat.com>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>

Reviewed-by: Michael Roth <mdroth@linux.vnet.ibm.com>

> ---
>  qga/commands-win32.c | 19 ++++++++++++++++---
>  1 file changed, 16 insertions(+), 3 deletions(-)
> 
> diff --git a/qga/commands-win32.c b/qga/commands-win32.c
> index ae8cf3d..09ed465 100644
> --- a/qga/commands-win32.c
> +++ b/qga/commands-win32.c
> @@ -1292,8 +1292,9 @@ void qmp_guest_set_user_password(const char *username,
>      NET_API_STATUS nas;
>      char *rawpasswddata = NULL;
>      size_t rawpasswdlen;
> -    wchar_t *user, *wpass;
> +    wchar_t *user = NULL, *wpass = NULL;
>      USER_INFO_1003 pi1003 = { 0, };
> +    GError *gerr = NULL;
> 
>      if (crypted) {
>          error_setg(errp, QERR_UNSUPPORTED);
> @@ -1307,8 +1308,15 @@ void qmp_guest_set_user_password(const char *username,
>      rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1);
>      rawpasswddata[rawpasswdlen] = '\0';
> 
> -    user = g_utf8_to_utf16(username, -1, NULL, NULL, NULL);
> -    wpass = g_utf8_to_utf16(rawpasswddata, -1, NULL, NULL, NULL);
> +    user = g_utf8_to_utf16(username, -1, NULL, NULL, &gerr);
> +    if (!user) {
> +        goto done;
> +    }
> +
> +    wpass = g_utf8_to_utf16(rawpasswddata, -1, NULL, NULL, &gerr);
> +    if (!wpass) {
> +        goto done;
> +    }
> 
>      pi1003.usri1003_password = wpass;
>      nas = NetUserSetInfo(NULL, user,
> @@ -1321,6 +1329,11 @@ void qmp_guest_set_user_password(const char *username,
>          g_free(msg);
>      }
> 
> +done:
> +    if (gerr) {
> +        error_setg(errp, QERR_QGA_COMMAND_FAILED, gerr->message);
> +        g_error_free(gerr);
> +    }
>      g_free(user);
>      g_free(wpass);
>      g_free(rawpasswddata);
> -- 
> 2.5.0
>
diff mbox

Patch

diff --git a/qga/commands-win32.c b/qga/commands-win32.c
index ae8cf3d..09ed465 100644
--- a/qga/commands-win32.c
+++ b/qga/commands-win32.c
@@ -1292,8 +1292,9 @@  void qmp_guest_set_user_password(const char *username,
     NET_API_STATUS nas;
     char *rawpasswddata = NULL;
     size_t rawpasswdlen;
-    wchar_t *user, *wpass;
+    wchar_t *user = NULL, *wpass = NULL;
     USER_INFO_1003 pi1003 = { 0, };
+    GError *gerr = NULL;
 
     if (crypted) {
         error_setg(errp, QERR_UNSUPPORTED);
@@ -1307,8 +1308,15 @@  void qmp_guest_set_user_password(const char *username,
     rawpasswddata = g_renew(char, rawpasswddata, rawpasswdlen + 1);
     rawpasswddata[rawpasswdlen] = '\0';
 
-    user = g_utf8_to_utf16(username, -1, NULL, NULL, NULL);
-    wpass = g_utf8_to_utf16(rawpasswddata, -1, NULL, NULL, NULL);
+    user = g_utf8_to_utf16(username, -1, NULL, NULL, &gerr);
+    if (!user) {
+        goto done;
+    }
+
+    wpass = g_utf8_to_utf16(rawpasswddata, -1, NULL, NULL, &gerr);
+    if (!wpass) {
+        goto done;
+    }
 
     pi1003.usri1003_password = wpass;
     nas = NetUserSetInfo(NULL, user,
@@ -1321,6 +1329,11 @@  void qmp_guest_set_user_password(const char *username,
         g_free(msg);
     }
 
+done:
+    if (gerr) {
+        error_setg(errp, QERR_QGA_COMMAND_FAILED, gerr->message);
+        g_error_free(gerr);
+    }
     g_free(user);
     g_free(wpass);
     g_free(rawpasswddata);