[libzomg] Metadata_win32.cpp: Remove trailing NULL bytes from the username.
authorDavid Korth <gerbilsoft@gerbilsoft.com>
Sun, 6 Sep 2015 19:13:43 +0000 (15:13 -0400)
committerDavid Korth <gerbilsoft@gerbilsoft.com>
Sun, 6 Sep 2015 19:13:43 +0000 (15:13 -0400)
GetUserName() and GetUserNameEx() return NULL-terminated strings.
We shouldn't include the NULL terminator; otherwise, C stdio will
stop printing the string data once it encounters the NULL.

This broke the PrintMetadata test on Windows 98SE, since fputs()
stopped printing text once it reached the NULL byte in the username.

Sidenote: While GetUserNameEx() returns a NULL-terminated string,
lpnSize doesn't include the NULL terminator. This is why it worked
correctly on Windows XP, but not Windows 98SE.

src/libzomg/Metadata_win32.cpp

index 725f7ae..739fccc 100644 (file)
@@ -141,6 +141,12 @@ static string getUserName_unicode(void)
                }
        }
 
+       // Remove any trailing NULL characters.
+       if (username[cchUsername-1] == 0) {
+               // Found a trailing NULL characters.
+               cchUsername--;
+       }
+
        if (cchUsername > 0) {
                // Try to convert from UTF-16 to UTF-8.
                // FIXME: If this fails, do a naive low-byte conversion?
@@ -182,6 +188,12 @@ static string getUserName_ansi(void)
                cbUsername = 0;
        }
 
+       // Remove any trailing NULL bytes.
+       if (username[cbUsername-1] == 0) {
+               // Found a trailing NULL byte.
+               cbUsername--;
+       }
+
        if (cbUsername > 0) {
                // Try to convert from ANSI to UTF-8.
                // FIXME: If this fails, use the ANSI text as-is?