FILE* fkey1, FILE* fkey2) {
struct loc_writer* w = calloc(1, sizeof(*w));
if (!w)
- return -ENOMEM;
+ return 1;
w->ctx = loc_ref(ctx);
w->refcount = 1;
loc_network_tree_unref(writer->networks);
// Unref the string pool
- loc_stringpool_unref(writer->pool);
+ if (writer->pool)
+ loc_stringpool_unref(writer->pool);
loc_unref(writer->ctx);
free(writer);
static int loc_writer_create_signature(struct loc_writer* writer,
struct loc_database_header_v1* header, FILE* f, EVP_PKEY* private_key,
char* signature, size_t* length) {
+ size_t bytes_read = 0;
+
DEBUG(writer->ctx, "Creating signature...\n");
// Read file from the beginning
// Read magic
struct loc_database_magic magic;
- fread(&magic, 1, sizeof(magic), f);
+ bytes_read = fread(&magic, 1, sizeof(magic), f);
+ if (bytes_read < sizeof(magic)) {
+ ERROR(writer->ctx, "Could not read header: %m\n");
+ r = 1;
+ goto END;
+ }
hexdump(writer->ctx, &magic, sizeof(magic));
// Walk through the file in chunks of 64kB
char buffer[64 * 1024];
while (!feof(f)) {
- size_t bytes_read = fread(buffer, 1, sizeof(buffer), f);
+ bytes_read = fread(buffer, 1, sizeof(buffer), f);
if (ferror(f)) {
- ERROR(writer->ctx, "Error reading from file: %s\n", strerror(errno));
- r = errno;
+ ERROR(writer->ctx, "Error reading from file: %m\n");
+ r = 1;
goto END;
}
fwrite(&header, 1, sizeof(header), f);
+ // Flush everything
+ fflush(f);
+
return r;
}