un = GNUNET_new (struct sockaddr_un);
un->sun_family = AF_UNIX;
- strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
+ GNUNET_strlcpy (un->sun_path, unixpath, sizeof (un->sun_path));
#ifdef LINUX
if (GNUNET_YES == abstract)
un->sun_path[0] = '\0';
return "NULL";
- strncpy (buf,
- ret,
- sizeof (buf) - 1);
+ GNUNET_strlcpy (buf,
+ ret,
+ sizeof (buf));
GNUNET_free (ret);
- buf[4] = '\0';
return buf;
}
*/
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
+
+/**
+ * Like strlcpy but portable. The given string @a src is copied in full length
+ * (until its null byte). The destination buffer is guaranteed to be
+ * null-terminated.
+ *
+ * to a destination buffer
+ * and ensures that the destination string is null-terminated.
+ *
+ * @param dst destination of the copy
+ * @param src source of the copy, must be null-terminated
+ * @param n the length of the string to copy, including its terminating null
+ * byte
+ * @return the length of the string that was copied, excluding the terminating
+ * null byte
+ */
+size_t
+GNUNET_strlcpy(char *dst, const char *src, size_t n)
+{
+ size_t ret;
+ size_t slen;
+
+ GNUNET_assert (0 != n);
+ ret = strlen (src);
+ slen = GNUNET_MIN (ret, n - 1);
+ memcpy (dst, src, slen);
+ dst[slen] = '\0';
+ return ret;
+}
+
+
/**
* Determines if the host OS is win32 or win64
*
* Set the device's hardware ID and add it to a list.
* This information will later on identify this device in registry.
*/
- strncpy (hwidlist, HARDWARE_ID, LINE_LEN);
+ str_length = GNUNET_strlcpy (hwidlist,
+ HARDWARE_ID,
+ sizeof (hwidlist)) + 1;
/**
* this is kind of over-complicated, but allows keeps things independent of
* how the openvpn-hwid is actually stored.
* A HWID list is double-\0 terminated and \0 separated
*/
str_length = strlen (hwidlist) + 1;
- strncpy (&hwidlist[str_length], secondary_hwid, LINE_LEN);
- str_length += strlen (&hwidlist[str_length]) + 1;
+ str_length += GNUNET_strlcpy (&hwidlist[str_length],
+ secondary_hwid,
+ sizeof (hwidlist) - str_length) + 1;
+ GNUNET_assert (str_length < sizeof (hwidlist));
+ hwidlist[str_length] = '\0';
+ ++str_length;
/**
* Locate the inf-file, we need to store it somewhere where the system can
/*
* we have successfully found OUR instance,
* save the device GUID before exiting
+ *
+ * We can use GNUNET_strlcpy here because instance key is null-
+ * terminated by RegEnumKeyExA.
*/
-
- strncpy (device_guid, instance_key, 256);
+ GNUNET_strlcpy (device_guid, instance_key, sizeof (device_guid));
retval = TRUE;
fprintf (stderr, "DEBUG: Interface Name lookup succeeded on retry %d, got \"%s\" %s\n", retrys, device_visible_name, device_guid);
return 1;
}
- strncpy (hwid, argv[1], LINE_LEN);
+ GNUNET_strlcpy (hwid, argv[1], sizeof (hwid));
hwid[LINE_LEN - 1] = '\0';
/*
char *suffix;
suffix = GNUNET_malloc (len - prefix_l + 1);
- strncpy (suffix, &ctx->s[prefix_l], len - prefix_l + 1);
+ /*
+ * We can use GNUNET_strlcpy because ctx->s is null-terminated
+ */
+ GNUNET_strlcpy (suffix, &ctx->s[prefix_l], len - prefix_l + 1);
/* Suffix saved, truncate current node so it only contains the prefix,
* copy any children nodes to put as grandchildren and initialize new empty
if (0 != hostname_size)
{
hostname = GNUNET_malloc (hostname_size + 1);
+ /* intentionally use strncpy (hostname not null terminated) */
(void) strncpy (hostname, ((char *) &msg[1]) + trusted_ip_size + 1,
hostname_size);
hostname[hostname_size] = '\0';
if (0 != username_length)
{
username = GNUNET_malloc (username_length + 1);
+ /* intentionally use strncpy (message payload is not null terminated) */
strncpy (username, ptr, username_length);
ptr += username_length;
}
hostname = GNUNET_malloc (hostname_length + 1);
+ /* intentionally use strncpy (message payload is not null terminated) */
strncpy (hostname,
ptr,
hostname_length);
rocc->a_id = msg->peer_identity;
GNUNET_TESTING_peer_get_identity (peer->details.local.peer,
&pid);
- (void) strncpy (pid_str,
- GNUNET_i2s (&pid),
- 15);
+ (void) GNUNET_strlcpy (pid_str,
+ GNUNET_i2s (&pid),
+ sizeof (pid_str));
LOG_DEBUG ("0x%llx: Remote overlay connect %s to peer %s with hello size: %u\n",
rocc->op_id,
pid_str,
msg->config_size = htons (config_size);
(void) strcpy ((char *) &msg[1], trusted_ip);
if (0 != hostname_len)
+ /* intentionally use strncpy (no null byte needed) */
(void) strncpy (((char *) &msg[1]) + trusted_ip_len + 1, hostname,
hostname_len);
return msg;
{
size = pmatch[2].rm_eo - pmatch[2].rm_so;
username = GNUNET_malloc (size + 1);
+ /*
+ * Intentionally use strncpy (buf is not necessarily null-terminated)
+ */
username[size] = '\0';
GNUNET_assert (NULL != strncpy (username, buf + pmatch[2].rm_so, size));
}
}
size = pmatch[3].rm_eo - pmatch[3].rm_so;
hostname = GNUNET_malloc (size + 1);
+ /*
+ * Intentionally use strncpy (buf is not necessarily null-terminated)
+ */
hostname[size] = '\0';
GNUNET_assert (NULL != strncpy (hostname, buf + pmatch[3].rm_so, size));
LOG (GNUNET_ERROR_TYPE_DEBUG,
GNUNET_assert (0 < strlen (unixpath)); /* sanity check */
un = GNUNET_new (struct sockaddr_un);
un->sun_family = AF_UNIX;
- strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
+ GNUNET_strlcpy (un->sun_path, unixpath, sizeof (un->sun_path));
#ifdef LINUX
{
int abstract;
un = GNUNET_new (struct sockaddr_un);
un->sun_family = AF_UNIX;
- strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
+ GNUNET_strlcpy (un->sun_path, unixpath, sizeof (un->sun_path));
#ifdef LINUX
if (GNUNET_YES == abstract)
un->sun_path[0] = '\0';
0,
sizeof (s_un));
s_un.sun_family = AF_UNIX;
- strncpy (s_un.sun_path,
- unixpath,
- sizeof (s_un.sun_path) - 1);
+ GNUNET_strlcpy (s_un.sun_path,
+ unixpath,
+ sizeof (s_un.sun_path));
#ifdef LINUX
{
int abstract;
return;
}
flush_bulk (date);
- strncpy (last_bulk, buf, sizeof (last_bulk));
+ GNUNET_strlcpy (last_bulk, buf, sizeof (last_bulk));
last_bulk_repeat = 0;
last_bulk_kind = kind;
last_bulk_time = GNUNET_TIME_absolute_get ();
- strncpy (last_bulk_comp, comp, COMP_TRACK_SIZE);
+ GNUNET_strlcpy (last_bulk_comp, comp, sizeof (last_bulk_comp));
output_message (kind, comp, date, buf);
}
}
if (NULL == pid)
return "NULL";
ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
- strncpy (buf, ret, sizeof (buf) - 1);
+ GNUNET_strlcpy (buf, ret, sizeof (buf));
GNUNET_free (ret);
- buf[4] = '\0';
return buf;
}
if (NULL == pid)
return "NULL";
ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&pid->public_key);
- strncpy (buf, ret, sizeof (buf) - 1);
+ GNUNET_strlcpy (buf, ret, sizeof (buf));
GNUNET_free (ret);
- buf[4] = '\0';
return buf;
}
}
if (NULL != prefix)
{
- strncpy (vanity, prefix, KEY_STR_LEN);
+ GNUNET_strlcpy (vanity, prefix, sizeof (vanity));
len = GNUNET_MIN (strlen (prefix), KEY_STR_LEN);
n = len * 5 / 8;
rest = len * 5 % 8;
un = GNUNET_new (struct sockaddr_un);
un->sun_family = AF_UNIX;
- strncpy (un->sun_path,
- unixpath,
- sizeof (un->sun_path) - 1);
+ GNUNET_strlcpy (un->sun_path,
+ unixpath,
+ sizeof (un->sun_path));
#ifdef LINUX
if (GNUNET_YES == abstract)
un->sun_path[0] = '\0';
/**
* Encode a string for the SOCKS5 protocol by prefixing it a byte stating its
- * length and stipping the trailing zero byte. Truncates any string longer
+ * length and stripping the trailing zero byte. Truncates any string longer
* than 255 bytes.
*
* @param b buffer to contain the encoded string
l=255;
}
*(b++) = (unsigned char) l;
- strncpy ((char *)b, s, l);
+ /*
+ * intentionally use strncpy (trailing zero byte must be stripped in b)
+ */
+ strncpy ((char*)b, s, l);
return b+l;
}
*/
void
GNUNET_SOCKS_set_handshake_destination (struct GNUNET_SOCKS_Handshake *ih,
- const char *host, uint16_t port)
+ const char *host, uint16_t port)
{
union {
struct in_addr in4;
*/
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
+
+/**
+ * Like strlcpy but portable. The given string @a src is copied in full length
+ * (until its null byte). The destination buffer is guaranteed to be
+ * null-terminated.
+ *
+ * to a destination buffer
+ * and ensures that the destination string is null-terminated.
+ *
+ * @param dst destination of the copy
+ * @param src source of the copy, must be null-terminated
+ * @param n the length of the string to copy, including its terminating null
+ * byte
+ * @return the length of the string that was copied, excluding the terminating
+ * null byte
+ */
+size_t
+GNUNET_strlcpy(char *dst, const char *src, size_t n)
+{
+ size_t ret;
+ size_t slen;
+
+ GNUNET_assert (0 != n);
+ ret = strlen (src);
+ slen = GNUNET_MIN (ret, n - 1);
+ memcpy (dst, src, slen);
+ dst[slen] = '\0';
+ return ret;
+}
+
+
/**
* Determines if the host OS is win32 or win64
*
* Set the device's hardware ID and add it to a list.
* This information will later on identify this device in registry.
*/
- strncpy (hwidlist, HARDWARE_ID, LINE_LEN);
+ str_len = GNUNET_strlcpy (hwidlist,
+ HARDWARE_ID,
+ sizeof (hwidList)) + 1;
/**
* this is kind of over-complicated, but allows keeps things independent of
* how the openvpn-hwid is actually stored.
*
* A HWID list is double-\0 terminated and \0 separated
*/
- str_length = strlen (hwidlist) + 1;
- strncpy (&hwidlist[str_length], secondary_hwid, LINE_LEN);
- str_length += strlen (&hwidlist[str_length]) + 1;
+ str_len += GNUNET_strlcpy (&hwidlist[str_length],
+ secondary_hwid,
+ sizeof (hwidlist) - str_len) + 1;
+ GNUNET_assert (str_len < sizeof (hwidlist));
+ hwidlist[str_len] = '\0';
+ ++str_len;
/**
* Locate the inf-file, we need to store it somewhere where the system can
/*
* we have successfully found OUR instance,
* save the device GUID before exiting
+ *
+ * We can use GNUNET_strlcpy here because instance key is null-
+ * terminated by RegEnumKeyExA.
*/
-
- strncpy (device_guid, instance_key, 256);
+ GNUNET_strlcpy (device_guid, instance_key, sizeof (device_guid));
retval = TRUE;
fprintf (stderr, "DEBUG: Interface Name lookup succeeded on retry %d, got \"%s\" %s\n", retrys, device_visible_name, device_guid);
return 1;
}
- strncpy (hwid, argv[1], LINE_LEN);
+ GNUNET_strlcpy (hwid, argv[1], sizeof (hwid));
hwid[LINE_LEN - 1] = '\0';
/*