Remove use of strcpy() and sprintf().

Even though they were safe, compilers like to warn about them nowadays.
This commit is contained in:
Guus Sliepen 2016-04-15 11:25:18 +02:00
parent d4410d0cce
commit cd5f222cc4
5 changed files with 12 additions and 10 deletions

View file

@ -121,11 +121,11 @@ void make_names(bool daemon) {
if(!unixsocketname) { if(!unixsocketname) {
int len = strlen(pidfilename); int len = strlen(pidfilename);
unixsocketname = xmalloc(len + 8); unixsocketname = xmalloc(len + 8);
strcpy(unixsocketname, pidfilename); memcpy(unixsocketname, pidfilename, len);
if(len > 4 && !strcmp(pidfilename + len - 4, ".pid")) if(len > 4 && !strcmp(pidfilename + len - 4, ".pid"))
strcpy(unixsocketname + len - 4, ".socket"); strncpy(unixsocketname + len - 4, ".socket", 8);
else else
strcpy(unixsocketname + len, ".socket"); strncpy(unixsocketname + len, ".socket", 8);
} }
} }

View file

@ -186,7 +186,7 @@ bool dump_nodes(connection_t *c) {
for splay_each(node_t, n, node_tree) { for splay_each(node_t, n, node_tree) {
char id[2 * sizeof n->id + 1]; char id[2 * sizeof n->id + 1];
for (size_t c = 0; c < sizeof n->id; ++c) for (size_t c = 0; c < sizeof n->id; ++c)
sprintf(id + 2 * c, "%02hhx", n->id.x[c]); snprintf(id + 2 * c, 3, "%02hhx", n->id.x[c]);
id[sizeof id - 1] = 0; id[sizeof id - 1] = 0;
send_request(c, "%d %d %s %s %s %d %d %d %d %x %x %s %s %d %hd %hd %hd %ld", CONTROL, REQ_DUMP_NODES, send_request(c, "%d %d %s %s %s %d %d %d %d %x %x %s %s %d %hd %hd %hd %ld", CONTROL, REQ_DUMP_NODES,
n->name, id, n->hostname ?: "unknown port unknown", n->name, id, n->hostname ?: "unknown port unknown",

View file

@ -75,9 +75,11 @@ bool execute_script(const char *name, char **envp) {
#ifdef HAVE_MINGW #ifdef HAVE_MINGW
if(!*scriptextension) { if(!*scriptextension) {
const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD"; const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD";
char fullname[strlen(scriptname) + strlen(pathext)]; size_t pathlen = strlen(pathext);
char *ext = fullname + strlen(scriptname); size_t scriptlen = strlen(scriptname);
strcpy(fullname, scriptname); char fullname[scriptlen + pathlen + 1];
char *ext = fullname + scriptlen;
strncpy(fullname, scriptname, sizeof fullname);
const char *p = pathext; const char *p = pathext;
bool found = false; bool found = false;
@ -88,7 +90,7 @@ bool execute_script(const char *name, char **envp) {
ext[q - p] = 0; ext[q - p] = 0;
q++; q++;
} else { } else {
strcpy(ext, p); strncpy(ext, p, pathlen + 1);
} }
if((found = !access(fullname, F_OK))) if((found = !access(fullname, F_OK)))
break; break;

View file

@ -204,7 +204,7 @@ static bool generate_key_material(sptps_t *s, const char *shared, size_t len) {
// Create the HMAC seed, which is "key expansion" + session label + server nonce + client nonce // Create the HMAC seed, which is "key expansion" + session label + server nonce + client nonce
char seed[s->labellen + 64 + 13]; char seed[s->labellen + 64 + 13];
strcpy(seed, "key expansion"); memcpy(seed, "key expansion", 13);
if(s->initiator) { if(s->initiator) {
memcpy(seed + 13, s->mykex + 1, 32); memcpy(seed + 13, s->mykex + 1, 32);
memcpy(seed + 45, s->hiskex + 1, 32); memcpy(seed + 45, s->hiskex + 1, 32);

View file

@ -158,7 +158,7 @@ int b64encode_urlsafe(const void *src, char *dst, int length) {
const char *winerror(int err) { const char *winerror(int err) {
static char buf[1024], *ptr; static char buf[1024], *ptr;
ptr = buf + sprintf(buf, "(%d) ", err); ptr = buf + snprintf(buf, sizeof buf, "(%d) ", err);
if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) { NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {