From 165005742c1c6a5b0b6d037c9e83cbe4a18a3092 Mon Sep 17 00:00:00 2001
From: "A. Wilcox" <AWilcox@Wilcox-Tech.com>
Date: Sun, 29 May 2022 03:37:26 -0500
Subject: [PATCH] persist: Fix ID storage on big-endian systems
Signed-off-by: A. Wilcox <AWilcox@Wilcox-Tech.com>
---
src/persist_read.c | 3 ++-
src/persist_read_v234.c | 5 ++++-
src/persist_read_v5.c | 4 ++++
src/persist_write.c | 4 ++--
src/persist_write_v5.c | 4 ++++
5 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/src/persist_read.c b/src/persist_read.c
index 5e7be454..e2b4ecb3 100644
--- a/src/persist_read.c
+++ b/src/persist_read.c
@@ -24,6 +24,7 @@ Contributors:
#include <arpa/inet.h>
#endif
#include <assert.h>
+#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@@ -480,7 +481,7 @@ int persist__restore(void)
fclose(fptr);
return 1;
}
- db.last_db_id = cfg_chunk.last_db_id;
+ db.last_db_id = le64toh(cfg_chunk.last_db_id);
break;
case DB_CHUNK_MSG_STORE:
diff --git a/src/persist_read_v234.c b/src/persist_read_v234.c
index 7460c309..701d88d9 100644
--- a/src/persist_read_v234.c
+++ b/src/persist_read_v234.c
@@ -24,6 +24,7 @@ Contributors:
#include <arpa/inet.h>
#endif
#include <assert.h>
+#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@@ -108,6 +109,7 @@ int persist__chunk_client_msg_read_v234(FILE *db_fptr, struct P_client_msg *chun
}
read_e(db_fptr, &chunk->F.store_id, sizeof(dbid_t));
+ chunk->F.store_id = le64toh(chunk->F.store_id);
read_e(db_fptr, &i16temp, sizeof(uint16_t));
chunk->F.mid = ntohs(i16temp);
@@ -137,6 +139,7 @@ int persist__chunk_msg_store_read_v234(FILE *db_fptr, struct P_msg_store *chunk,
char *err;
read_e(db_fptr, &chunk->F.store_id, sizeof(dbid_t));
+ chunk->F.store_id = le64toh(chunk->F.store_id);
rc = persist__read_string(db_fptr, &chunk->source.id);
if(rc){
@@ -205,7 +208,7 @@ int persist__chunk_retain_read_v234(FILE *db_fptr, struct P_retain *chunk)
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
return 1;
}
- chunk->F.store_id = i64temp;
+ chunk->F.store_id = le64toh(i64temp);
return MOSQ_ERR_SUCCESS;
}
diff --git a/src/persist_read_v5.c b/src/persist_read_v5.c
index abc9a580..12d3e801 100644
--- a/src/persist_read_v5.c
+++ b/src/persist_read_v5.c
@@ -24,6 +24,7 @@ Contributors:
#include <arpa/inet.h>
#endif
#include <assert.h>
+#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@@ -116,6 +117,7 @@ int persist__chunk_client_msg_read_v56(FILE *db_fptr, struct P_client_msg *chunk
read_e(db_fptr, &chunk->F, sizeof(struct PF_client_msg));
chunk->F.mid = ntohs(chunk->F.mid);
chunk->F.id_len = ntohs(chunk->F.id_len);
+ chunk->F.store_id = le64toh(chunk->F.store_id);
length -= (uint32_t)(sizeof(struct PF_client_msg) + chunk->F.id_len);
@@ -165,6 +167,7 @@ int persist__chunk_msg_store_read_v56(FILE *db_fptr, struct P_msg_store *chunk,
chunk->F.source_username_len = ntohs(chunk->F.source_username_len);
chunk->F.topic_len = ntohs(chunk->F.topic_len);
chunk->F.source_port = ntohs(chunk->F.source_port);
+ chunk->F.store_id = le64toh(chunk->F.store_id);
length -= (uint32_t)(sizeof(struct PF_msg_store) + chunk->F.payloadlen + chunk->F.source_id_len + chunk->F.source_username_len + chunk->F.topic_len);
@@ -246,6 +249,7 @@ int persist__chunk_retain_read_v56(FILE *db_fptr, struct P_retain *chunk)
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", strerror(errno));
return 1;
}
+ chunk->F.store_id = le64toh(chunk->F.store_id);
return MOSQ_ERR_SUCCESS;
}
diff --git a/src/persist_write.c b/src/persist_write.c
index ccfbc29c..93a32639 100644
--- a/src/persist_write.c
+++ b/src/persist_write.c
@@ -273,7 +273,7 @@ static int persist__retain_save(FILE *db_fptr, struct mosquitto__retainhier *nod
if(node->retained && strncmp(node->retained->topic, "$SYS", 4)){
/* Don't save $SYS messages. */
- retain_chunk.F.store_id = node->retained->db_id;
+ retain_chunk.F.store_id = htole64(node->retained->db_id);
rc = persist__chunk_retain_write_v6(db_fptr, &retain_chunk);
if(rc){
return rc;
@@ -367,7 +367,7 @@ int persist__backup(bool shutdown)
write_e(db_fptr, &db_version_w, sizeof(uint32_t));
memset(&cfg_chunk, 0, sizeof(struct PF_cfg));
- cfg_chunk.last_db_id = db.last_db_id;
+ cfg_chunk.last_db_id = htole64(db.last_db_id);
cfg_chunk.shutdown = shutdown;
cfg_chunk.dbid_size = sizeof(dbid_t);
if(persist__chunk_cfg_write_v6(db_fptr, &cfg_chunk)){
diff --git a/src/persist_write_v5.c b/src/persist_write_v5.c
index 8c9f6c34..b9fb5a8e 100644
--- a/src/persist_write_v5.c
+++ b/src/persist_write_v5.c
@@ -24,6 +24,7 @@ Contributors:
#include <arpa/inet.h>
#endif
#include <assert.h>
+#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@@ -100,6 +101,7 @@ int persist__chunk_client_msg_write_v6(FILE *db_fptr, struct P_client_msg *chunk
chunk->F.mid = htons(chunk->F.mid);
chunk->F.id_len = htons(chunk->F.id_len);
+ chunk->F.store_id = htole64(chunk->F.store_id);
header.chunk = htonl(DB_CHUNK_CLIENT_MSG);
header.length = htonl((uint32_t)sizeof(struct PF_client_msg) + id_len + proplen);
@@ -149,6 +151,7 @@ int persist__chunk_message_store_write_v6(FILE *db_fptr, struct P_msg_store *chu
proplen += property__get_remaining_length(chunk->properties);
}
+ chunk->F.store_id = htole64(chunk->F.store_id);
chunk->F.payloadlen = htonl(chunk->F.payloadlen);
chunk->F.source_mid = htons(chunk->F.source_mid);
chunk->F.source_id_len = htons(chunk->F.source_id_len);
@@ -206,6 +209,7 @@ int persist__chunk_retain_write_v6(FILE *db_fptr, struct P_retain *chunk)
header.chunk = htonl(DB_CHUNK_RETAIN);
header.length = htonl((uint32_t)sizeof(struct PF_retain));
+ chunk->F.store_id = htole64(chunk->F.store_id);
write_e(db_fptr, &header, sizeof(struct PF_header));
write_e(db_fptr, &chunk->F, sizeof(struct PF_retain));
--
2.35.1