Its null-padding behavior can be surprising.
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);
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);
/* 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) {
/* 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);
}
/* 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) {
/* 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);
}
/* 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);
/* 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);
}
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);
}
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 {
if (strcmp (s, "ABCD") != 0)
return 1;
- strncpy(s, m, n);
+ memcpy(s, m, n);
s [n] = '\0';
return 0;
}
{
const char m[] = "This is my message. Isn't it nice?";
- strncpy(s, m, n);
+ memcpy(s, m, n);
s [n] = '\0';
return 0;
}
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;
}
size = 0;
} else {
size = counter->size;
- strncpy ((char *) buffer, strval, size - 1);
+ memcpy ((char *) buffer, strval, size - 1);
((char*)buffer)[size - 1] = '\0';
}
}
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;
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
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;
} 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__)
} 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)
} 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