]> git.meshlink.io Git - meshlink/blobdiff - src/meshlink.c
fix private key file read and write on windows (binary read/write)
[meshlink] / src / meshlink.c
index 918a62b6e500c8affcd01c3763e21cab27276247..cbe88a3ed256e06f25a72663fc60991ff4189af3 100644 (file)
@@ -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);
-       strncpy(hdr->destination, destination->name, sizeof hdr->destination);
-       strncpy(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;