From 78631a0da6b7593c30bed99ca9317df11fabbb90 Mon Sep 17 00:00:00 2001 From: Roger Light Date: Mon, 27 Apr 2026 09:16:04 +0100 Subject: [PATCH] Clients: Fix assumption that `long` is >32 bits. --- client/client_props.c | 17 +++++++++-------- client/client_shared.c | 22 ++++++++++++++-------- client/client_shared.h | 2 +- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/client/client_props.c b/client/client_props.c index 9260602ce..06a7be5ef 100644 --- a/client/client_props.c +++ b/client/client_props.c @@ -68,6 +68,7 @@ int cfg_parse_property(struct mosq_config *cfg, int argc, char *argv[], int *idx mosquitto_property **proplist; int rc; long tmpl; + long long tmpll; size_t szt; /* idx now points to "command" */ @@ -176,20 +177,20 @@ int cfg_parse_property(struct mosq_config *cfg, int argc, char *argv[], int *idx rc = mosquitto_property_add_int16(proplist, identifier, (uint16_t )tmpl); break; case MQTT_PROP_TYPE_INT32: - tmpl = atol(value); - if(tmpl < 0 || tmpl > UINT32_MAX){ - fprintf(stderr, "Error: Property value (%ld) out of range for property %s.\n\n", tmpl, propname); + tmpll = atoll(value); + if(tmpll < 0 || tmpll > UINT32_MAX){ + fprintf(stderr, "Error: Property value (%lld) out of range for property %s.\n\n", tmpll, propname); return MOSQ_ERR_INVAL; } - rc = mosquitto_property_add_int32(proplist, identifier, (uint32_t )tmpl); + rc = mosquitto_property_add_int32(proplist, identifier, (uint32_t )tmpll); break; case MQTT_PROP_TYPE_VARINT: - tmpl = atol(value); - if(tmpl < 0 || tmpl > UINT32_MAX){ - fprintf(stderr, "Error: Property value (%ld) out of range for property %s.\n\n", tmpl, propname); + tmpll = atoll(value); + if(tmpll < 0 || tmpll > UINT32_MAX){ + fprintf(stderr, "Error: Property value (%lld) out of range for property %s.\n\n", tmpll, propname); return MOSQ_ERR_INVAL; } - rc = mosquitto_property_add_varint(proplist, identifier, (uint32_t )tmpl); + rc = mosquitto_property_add_varint(proplist, identifier, (uint32_t )tmpll); break; case MQTT_PROP_TYPE_BINARY: szt = strlen(value); diff --git a/client/client_shared.c b/client/client_shared.c index 91827524a..84dc59e8b 100644 --- a/client/client_shared.c +++ b/client/client_shared.c @@ -226,7 +226,7 @@ static void init_config(struct mosq_config *cfg, int pub_or_sub) }else{ cfg->protocol_version = MQTT_PROTOCOL_V311; } - cfg->session_expiry_interval = -1; /* -1 means unset here, the user can't set it to -1. */ + cfg->session_expiry_interval = -2; /* -2 means unset here, the user can't set it to -2. */ cfg->transport = MOSQ_T_TCP; } @@ -481,10 +481,14 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char * #endif if(cfg->protocol_version == 5){ - if(cfg->clean_session == false && cfg->session_expiry_interval == -1){ - /* User hasn't set session-expiry-interval, but has cleared clean - * session so default to persistent session. */ - cfg->session_expiry_interval = UINT32_MAX; + if(cfg->session_expiry_interval == -2){ + if(cfg->clean_session == false){ + /* User hasn't set session-expiry-interval, but has cleared clean + * session so default to persistent session. */ + cfg->session_expiry_interval = UINT32_MAX; + }else{ + cfg->session_expiry_interval = 0; + } } if(cfg->session_expiry_interval > 0){ if(cfg->session_expiry_interval == UINT32_MAX && (cfg->id_prefix || !cfg->id)){ @@ -1310,19 +1314,21 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c cfg->session_expiry_interval = UINT32_MAX; }else{ char *endptr = NULL; - cfg->session_expiry_interval = strtol(argv[i+1], &endptr, 0); + long long llvalue = strtoll(argv[i+1], &endptr, 0); if(endptr == argv[i+1] || endptr[0] != '\0'){ /* Entirety of argument wasn't a number */ fprintf(stderr, "Error: session-expiry-interval not a number.\n\n"); return 1; } - if(cfg->session_expiry_interval > UINT32_MAX || cfg->session_expiry_interval < -1){ + if(llvalue > UINT32_MAX || llvalue < -1){ fprintf(stderr, "Error: session-expiry-interval out of range.\n\n"); return 1; } - if(cfg->session_expiry_interval == -1){ + if(llvalue == -1LL){ /* Convenience value for infinity. */ cfg->session_expiry_interval = UINT32_MAX; + }else{ + cfg->session_expiry_interval = llvalue; } } cfg->protocol_version = MQTT_PROTOCOL_V5; diff --git a/client/client_shared.h b/client/client_shared.h index 7a99c554c..f275b8fa6 100644 --- a/client/client_shared.h +++ b/client/client_shared.h @@ -119,7 +119,7 @@ struct mosq_config { bool pretty; /* sub, rr */ unsigned int timeout; /* sub */ int sub_opts; /* sub */ - long session_expiry_interval; + int64_t session_expiry_interval; int random_filter; /* sub */ int transport; #ifndef WIN32