[runtime] Avoid 'strncpy'.
authorJon Purdy <evincarofautumn@gmail.com>
Fri, 3 Feb 2017 00:37:46 +0000 (16:37 -0800)
committerJon Purdy <evincarofautumn@gmail.com>
Fri, 3 Feb 2017 21:33:47 +0000 (13:33 -0800)
Its null-padding behavior can be surprising.

mono/metadata/assembly.c
mono/metadata/w32event-unix.c
mono/metadata/w32mutex-unix.c
mono/metadata/w32semaphore-unix.c
mono/mini/debugger-agent.c
mono/mini/trace.c
mono/tests/libtest.c
mono/utils/mono-counters.c
mono/utils/mono-proclib.c
mono/utils/mono-rand.c
mono/utils/mono-threads-posix.c

index d83d40b13ea997c910eb9d64d8ea1f02e178057d..f16479158b865bdbe2bf5af1e1e8b6187a4234ab 100644 (file)
@@ -2826,7 +2826,7 @@ mono_assembly_load_publisher_policy (MonoAssemblyName *aname)
        if (strstr (aname->name, ".dll")) {
                len = strlen (aname->name) - 4;
                name = (gchar *)g_malloc (len + 1);
-               strncpy (name, aname->name, len);
+               memcpy (name, aname->name, len);
                name[len] = 0;
        } else
                name = g_strdup (aname->name);
@@ -3143,7 +3143,7 @@ mono_assembly_load_from_gac (MonoAssemblyName *aname,  gchar *filename, MonoImag
        if (strstr (aname->name, ".dll")) {
                len = strlen (filename) - 4;
                name = (gchar *)g_malloc (len + 1);
-               strncpy (name, aname->name, len);
+               memcpy (name, aname->name, len);
                name[len] = 0;
        } else {
                name = g_strdup (aname->name);
index 50e9a6abad471f205cca5cef0cc66dd9a39bcaa2..0300e204d7092f6a50ccc3eae69a4aec253cf4f4 100644 (file)
@@ -223,7 +223,8 @@ static gpointer namedevent_create (gboolean manual, gboolean initial, const guni
        /* w32 seems to guarantee that opening named objects can't race each other */
        mono_w32handle_namespace_lock ();
 
-       utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
+       glong utf8_len = 0;
+       utf8_name = g_utf16_to_utf8 (name, -1, NULL, &utf8_len, NULL);
 
        handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDEVENT, utf8_name);
        if (handle == INVALID_HANDLE_VALUE) {
@@ -239,8 +240,9 @@ static gpointer namedevent_create (gboolean manual, gboolean initial, const guni
                /* A new named event */
                MonoW32HandleNamedEvent namedevent_handle;
 
-               strncpy (&namedevent_handle.sharedns.name [0], utf8_name, MAX_PATH);
-               namedevent_handle.sharedns.name [MAX_PATH] = '\0';
+               size_t len = utf8_len < MAX_PATH ? utf8_len : MAX_PATH;
+               memcpy (&namedevent_handle.sharedns.name [0], utf8_name, len);
+               namedevent_handle.sharedns.name [len] = '\0';
 
                handle = event_handle_create ((MonoW32HandleEvent*) &namedevent_handle, MONO_W32HANDLE_NAMEDEVENT, manual, initial);
        }
index acde96a791b5de5568b91fce5bfe00edeb9c76d0..f98779da887c5ccfd82d17b9f2e5941b3d082106 100644 (file)
@@ -314,7 +314,8 @@ static gpointer namedmutex_create (gboolean owned, const gunichar2 *name)
        /* w32 seems to guarantee that opening named objects can't race each other */
        mono_w32handle_namespace_lock ();
 
-       utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
+       glong utf8_len = 0;
+       utf8_name = g_utf16_to_utf8 (name, -1, NULL, &utf8_len, NULL);
 
        handle = mono_w32handle_namespace_search_handle (MONO_W32HANDLE_NAMEDMUTEX, utf8_name);
        if (handle == INVALID_HANDLE_VALUE) {
@@ -330,8 +331,9 @@ static gpointer namedmutex_create (gboolean owned, const gunichar2 *name)
                /* A new named mutex */
                MonoW32HandleNamedMutex namedmutex_handle;
 
-               strncpy (&namedmutex_handle.sharedns.name [0], utf8_name, MAX_PATH);
-               namedmutex_handle.sharedns.name [MAX_PATH] = '\0';
+               size_t len = utf8_len < MAX_PATH ? utf8_len : MAX_PATH;
+               memcpy (&namedmutex_handle.sharedns.name [0], utf8_name, len);
+               namedmutex_handle.sharedns.name [len] = '\0';
 
                handle = mutex_handle_create ((MonoW32HandleMutex*) &namedmutex_handle, MONO_W32HANDLE_NAMEDMUTEX, owned);
        }
index 2537853943d81bb831a755c881cab5fb8b2f908a..b40a779cb4aca33f37a03ed5004c012f74a2bb16 100644 (file)
@@ -188,7 +188,8 @@ namedsem_create (gint32 initial, gint32 max, const gunichar2 *name)
        /* w32 seems to guarantee that opening named objects can't race each other */
        mono_w32handle_namespace_lock ();
 
-       utf8_name = g_utf16_to_utf8 (name, -1, NULL, NULL, NULL);
+       glong utf8_len = 0;
+       utf8_name = g_utf16_to_utf8 (name, -1, NULL, &utf8_len, NULL);
 
        mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Creating named sem name [%s] initial %d max %d", __func__, utf8_name, initial, max);
 
@@ -206,8 +207,9 @@ namedsem_create (gint32 initial, gint32 max, const gunichar2 *name)
                /* A new named semaphore */
                MonoW32HandleNamedSemaphore namedsem_handle;
 
-               strncpy (&namedsem_handle.sharedns.name [0], utf8_name, MAX_PATH);
-               namedsem_handle.sharedns.name [MAX_PATH] = '\0';
+               size_t len = utf8_len < MAX_PATH ? utf8_len : MAX_PATH;
+               memcpy (&namedsem_handle.sharedns.name [0], utf8_name, len);
+               namedsem_handle.sharedns.name [len] = '\0';
 
                handle = sem_handle_create ((MonoW32HandleSemaphore*) &namedsem_handle, MONO_W32HANDLE_NAMEDSEM, initial, max);
        }
index f64481d661fc599ae756a683e236674d61ba2d7d..11b7125b2b379a37c6acf4692320f992e8dfaa99 100644 (file)
@@ -830,9 +830,10 @@ parse_address (char *address, char **host, int *port)
        if (pos == NULL || pos == address)
                return 1;
 
-       *host = (char *)g_malloc (pos - address + 1);
-       strncpy (*host, address, pos - address);
-       (*host) [pos - address] = '\0';
+       size_t len = pos - address;
+       *host = (char *)g_malloc (len + 1);
+       memcpy (*host, address, len);
+       (*host) [len] = '\0';
 
        *port = atoi (pos + 1);
 
index 90f0af81b5a2c2e606f9e4b2d7be094f7be8d357..d44511d7739bd53613ff092c02264a509b73a3e7 100644 (file)
@@ -166,9 +166,10 @@ static void get_string (void)
        }
        if (value != NULL)
                g_free (value);
-       value = (char *)g_malloc (input - start + 1);
-       strncpy (value, start, input-start);
-       value [input-start] = 0;
+       size_t len = input - start;
+       value = (char *)g_malloc (len + 1);
+       memcpy (value, start, len);
+       value [len] = 0;
 }
 
 enum Token {
index ad4a8fc9bac3fbf0bbb0ef0cc4f894e35a9b6ecf..4517eda2f74f64ddc41f8e2453372f4b17e5fd12 100644 (file)
@@ -1132,7 +1132,7 @@ mono_test_marshal_stringbuilder (char *s, int n)
 
        if (strcmp (s, "ABCD") != 0)
                return 1;
-       strncpy(s, m, n);
+       memcpy(s, m, n);
        s [n] = '\0';
        return 0;
 }
@@ -1158,7 +1158,7 @@ mono_test_marshal_stringbuilder_default (char *s, int n)
 {
        const char m[] = "This is my message.  Isn't it nice?";
 
-       strncpy(s, m, n);
+       memcpy(s, m, n);
        s [n] = '\0';
        return 0;
 }
@@ -7283,8 +7283,8 @@ build_return_string(const char* pReturn)
 
        size_t strLength = strlen(pReturn);
        ret = (char *)(marshal_alloc (sizeof(char)* (strLength + 1)));
-       memset(ret, '\0', strLength + 1);
-       strncpy(ret, pReturn, strLength);
+       memcpy(ret, pReturn, strLength);
+       ret [strLength] = '\0';
        return ret;
 }
 
index f867960803ba46f7c81f828b8a4e56fc614fd382..e8ceb3927fcb2518b352ba5fd6335a72e0cc3ae2 100644 (file)
@@ -505,7 +505,7 @@ sample_internal (MonoCounter *counter, void *buffer, int buffer_size)
                                size = 0;
                        } else {
                                size = counter->size;
-                               strncpy ((char *) buffer, strval, size - 1);
+                               memcpy ((char *) buffer, strval, size - 1);
                                ((char*)buffer)[size - 1] = '\0';
                        }
                }
index f1a28444425eadb87c55a6626e1f70d173458e8f..515104efaf645556379eb2cc0f7354f1b820ef69 100644 (file)
@@ -218,7 +218,7 @@ get_pid_status_item_buf (int pid, const char *item, char *rbuf, int blen, MonoPr
                while (g_ascii_isspace (*s)) s++;
                fclose (f);
                len = strlen (s);
-               strncpy (rbuf, s, MIN (len, blen));
+               memcpy (rbuf, s, MIN (len, blen));
                rbuf [MIN (len, blen) - 1] = 0;
                if (error)
                        *error = MONO_PROCESS_ERROR_NONE;
@@ -289,7 +289,7 @@ mono_process_get_name (gpointer pid, char *buf, int len)
        memset (buf, 0, len);
 
        if (sysctl_kinfo_proc (pid, &processi))
-               strncpy (buf, processi.kinfo_name_member, len - 1);
+               memcpy (buf, processi.kinfo_name_member, len - 1);
 
        return buf;
 #else
index 1ac91179382d2ffb4ee7f5e826bca141e7cc4bb5..884dc7f1600775ab0a2474b267f835f7ec23739a 100644 (file)
@@ -59,7 +59,7 @@ get_entropy_from_egd (const char *path, guchar *buffer, int buffer_size, MonoErr
                err = errno;
        } else {
                egd_addr.sun_family = AF_UNIX;
-               strncpy (egd_addr.sun_path, path, sizeof (egd_addr.sun_path) - 1);
+               memcpy (egd_addr.sun_path, path, sizeof (egd_addr.sun_path) - 1);
                egd_addr.sun_path [sizeof (egd_addr.sun_path) - 1] = '\0';
                ret = connect (socket_fd, (struct sockaddr*) &egd_addr, sizeof (egd_addr));
                err = errno;
index 430738591c6884de507fc93103a6a150f2edcd5b..dcb7e27d2a1e16503f6d8d897f55593dd4421e3e 100644 (file)
@@ -240,8 +240,8 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
        } else {
                char n [63];
 
-               strncpy (n, name, 63);
-               n [62] = '\0';
+               memcpy (n, name, sizeof (n) - 1);
+               n [sizeof (n) - 1] = '\0';
                pthread_setname_np (n);
        }
 #elif defined (__NetBSD__)
@@ -250,8 +250,8 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
        } else {
                char n [PTHREAD_MAX_NAMELEN_NP];
 
-               strncpy (n, name, PTHREAD_MAX_NAMELEN_NP);
-               n [PTHREAD_MAX_NAMELEN_NP - 1] = '\0';
+               memcpy (n, name, sizeof (n) - 1);
+               n [sizeof (n) - 1] = '\0';
                pthread_setname_np (tid, "%s", (void*)n);
        }
 #elif defined (HAVE_PTHREAD_SETNAME_NP)
@@ -260,8 +260,8 @@ mono_native_thread_set_name (MonoNativeThreadId tid, const char *name)
        } else {
                char n [16];
 
-               strncpy (n, name, 16);
-               n [15] = '\0';
+               memcpy (n, name, sizeof (n) - 1);
+               n [sizeof (n) - 1] = '\0';
                pthread_setname_np (tid, n);
        }
 #endif