X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=cbe88a3ed256e06f25a72663fc60991ff4189af3;hb=80d7a91d86f8cde7b1a550ef18ae7b84e2236ab6;hp=df695b15fa9855a6c5ab9bbee668b39b56cb0fc6;hpb=fe1a9111456a73a6e690927e90d8bfa1e1d0ad22;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index df695b15..cbe88a3e 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -660,7 +660,7 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) { logger(mesh, MESHLINK_DEBUG, "Done.\n"); snprintf(privname, sizeof privname, "%s" SLASH "ecdsa_key.priv", mesh->confbase); - f = fopen(privname, "w"); + f = fopen(privname, "wb"); if(!f) { meshlink_errno = MESHLINK_ESTORAGE; @@ -1050,8 +1050,10 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const hdr = (meshlink_packethdr_t *)packet->data; memset(hdr, 0, sizeof *hdr); - memcpy(hdr->destination, destination->name, sizeof hdr->destination); - memcpy(hdr->source, mesh->self->name, sizeof hdr->source); + // leave the last byte as 0 to make sure strings are always + // null-terminated if they are longer than the buffer + strncpy(hdr->destination, destination->name, (sizeof hdr->destination) - 1); + strncpy(hdr->source, mesh->self->name, (sizeof hdr->source) -1 ); memcpy(packet->data + sizeof *hdr, data, len); @@ -1290,7 +1292,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { } // Create a new key if necessary. - FILE *f = fopen(filename, "r"); + FILE *f = fopen(filename, "rb"); if(!f) { if(errno != ENOENT) { logger(mesh, MESHLINK_DEBUG, "Could not read %s: %s\n", filename, strerror(errno)); @@ -1306,7 +1308,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; } - f = fopen(filename, "w"); + f = fopen(filename, "wb"); if(!f) { logger(mesh, MESHLINK_DEBUG, "Could not write %s: %s\n", filename, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE;