Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
}
LOC_EXPORT int loc_database_verify(struct loc_database* db, FILE* f) {
}
LOC_EXPORT int loc_database_verify(struct loc_database* db, FILE* f) {
+ size_t bytes_read = 0;
+
// Cannot do this when no signature is available
if (!db->signature1 && !db->signature2) {
DEBUG(db->ctx, "No signature available to verify\n");
// Cannot do this when no signature is available
if (!db->signature1 && !db->signature2) {
DEBUG(db->ctx, "No signature available to verify\n");
// Read magic
struct loc_database_magic magic;
// Read magic
struct loc_database_magic magic;
- fread(&magic, 1, sizeof(magic), db->f);
+ bytes_read = fread(&magic, 1, sizeof(magic), db->f);
+ if (bytes_read < sizeof(magic)) {
+ ERROR(db->ctx, "Could not read header: %m\n");
+ r = 1;
+ goto CLEANUP;
+ }
hexdump(db->ctx, &magic, sizeof(magic));
hexdump(db->ctx, &magic, sizeof(magic));
// Read the header
struct loc_database_header_v1 header_v1;
// Read the header
struct loc_database_header_v1 header_v1;
switch (db->version) {
case LOC_DATABASE_VERSION_1:
switch (db->version) {
case LOC_DATABASE_VERSION_1:
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) {
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
DEBUG(writer->ctx, "Creating signature...\n");
// Read file from the beginning
// Read magic
struct loc_database_magic magic;
// 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));
hexdump(writer->ctx, &magic, sizeof(magic));
// Walk through the file in chunks of 64kB
char buffer[64 * 1024];
while (!feof(f)) {
// 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: %m\n");
if (ferror(f)) {
ERROR(writer->ctx, "Error reading from file: %m\n");