* new StrHelper::strzcpy()

This commit is contained in:
Scott Powell
2025-02-22 20:10:31 +11:00
parent 37f4ceff85
commit fc4e5ed54c
4 changed files with 13 additions and 3 deletions

View File

@@ -332,7 +332,7 @@ class MyMesh : public BaseChatMesh {
out_frame[i++] = contact.flags;
out_frame[i++] = contact.out_path_len;
memcpy(&out_frame[i], contact.out_path, MAX_PATH_SIZE); i += MAX_PATH_SIZE;
memcpy(&out_frame[i], contact.name, 32); i += 32;
StrHelper::strzcpy((char *) &out_frame[i], contact.name, 32); i += 32;
memcpy(&out_frame[i], &contact.last_advert_timestamp, 4); i += 4;
memcpy(&out_frame[i], &contact.gps_lat, 4); i += 4;
memcpy(&out_frame[i], &contact.gps_lon, 4); i += 4;

View File

@@ -498,11 +498,9 @@ public:
_prefs.rx_delay_base = 0.0f; // turn off by default, was 10.0;
_prefs.tx_delay_factor = 0.5f; // was 0.25f
StrHelper::strncpy(_prefs.node_name, ADVERT_NAME, sizeof(_prefs.node_name));
_prefs.node_name[sizeof(_prefs.node_name)-1] = 0; // truncate if necessary
_prefs.node_lat = ADVERT_LAT;
_prefs.node_lon = ADVERT_LON;
StrHelper::strncpy(_prefs.password, ADMIN_PASSWORD, sizeof(_prefs.password));
_prefs.password[sizeof(_prefs.password)-1] = 0; // truncate if necessary
_prefs.freq = LORA_FREQ;
_prefs.tx_power_dbm = LORA_TX_POWER;
_prefs.advert_interval = 1; // default to 2 minutes for NEW installs

View File

@@ -7,3 +7,14 @@ void StrHelper::strncpy(char* dest, const char* src, size_t buf_sz) {
}
*dest = 0; // truncates if needed
}
void StrHelper::strzcpy(char* dest, const char* src, size_t buf_sz) {
while (buf_sz > 1 && *src) {
*dest++ = *src++;
buf_sz--;
}
while (buf_sz > 0) { // pad remaining with nulls
*dest++ = 0;
buf_sz--;
}
}

View File

@@ -10,4 +10,5 @@
class StrHelper {
public:
static void strncpy(char* dest, const char* src, size_t buf_sz);
static void strzcpy(char* dest, const char* src, size_t buf_sz); // pads with trailing nulls
};