diff --git a/src/tapctl/main.c b/src/tapctl/main.c
index 6697d14..12ab270 100644
--- a/src/tapctl/main.c
+++ b/src/tapctl/main.c
@@ -512,11 +512,20 @@
     {
         if (wcsicmp(argv[i], L"--hwid") == 0)
         {
+            if (++i >= argc)
+            {
+                fwprintf(stderr, L"--hwid option requires a value. Ignored.\n");
+                break;
+            }
+            /* leave room for the two terminating zeros */
+            size_t len = wcslen(argv[i]);
+            if (len > _countof(szzHwId) - 2)
+            {
+                fwprintf(stderr, L"--hwid value is too long. Ignored.\n");
+                continue;
+            }
             memset(szzHwId, 0, sizeof(szzHwId));
-            ++i;
-            memcpy_s(szzHwId,
-                     sizeof(szzHwId) - 2 * sizeof(WCHAR),
-                     argv[i], wcslen(argv[i]) * sizeof(WCHAR));
+            wmemcpy(szzHwId, argv[i], len);
         }
         else
         {
