From e5ab9d74bdf0c2b9ecdec3bbccf77e7eb0b7c868 Mon Sep 17 00:00:00 2001
From: Selva Nair <selva.nair@gmail.com>
Date: Tue, 28 Jun 2022 20:37:50 -0400
Subject: [PATCH] Fix potential string mishandling in LocalizedTime

Signed-off-by: Selva Nair <selva.nair@gmail.com>
---
 localization.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/localization.c b/localization.c
index 6961727..b6ba51d 100644
--- a/localization.c
+++ b/localization.c
@@ -114,16 +114,17 @@ LocalizedSystemTime(const SYSTEMTIME *st, wchar_t *buf, size_t size)
         return date_size + time_size;
     }
 
-    if (size > 0) {
-        date_size = GetDateFormat(locale, DATE_SHORTDATE, st, NULL,
-                                  buf, size);
-        if (date_size)
-            buf[date_size - 1] = ' ';
-    }
-    if (size - date_size > 0) {
+    date_size = GetDateFormat(locale, DATE_SHORTDATE, st, NULL,
+                              buf, size);
+    if (size > (size_t) date_size)
+    {
         time_size = GetTimeFormat(locale, TIME_NOSECONDS, st, NULL,
                                   buf + date_size, size - date_size);
     }
+    if (date_size > 0 && time_size > 0)
+    {
+        buf[date_size - 1] = L' '; /* replaces the NUL char in the middle */
+    }
     return date_size + time_size;
 }