Clients: Fix assumption that long is >32 bits.

This commit is contained in:
Roger Light 2026-04-27 09:16:04 +01:00
parent f941f83f73
commit 78631a0da6
3 changed files with 24 additions and 17 deletions

View file

@ -68,6 +68,7 @@ int cfg_parse_property(struct mosq_config *cfg, int argc, char *argv[], int *idx
mosquitto_property **proplist; mosquitto_property **proplist;
int rc; int rc;
long tmpl; long tmpl;
long long tmpll;
size_t szt; size_t szt;
/* idx now points to "command" */ /* 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); rc = mosquitto_property_add_int16(proplist, identifier, (uint16_t )tmpl);
break; break;
case MQTT_PROP_TYPE_INT32: case MQTT_PROP_TYPE_INT32:
tmpl = atol(value); tmpll = atoll(value);
if(tmpl < 0 || tmpl > UINT32_MAX){ if(tmpll < 0 || tmpll > UINT32_MAX){
fprintf(stderr, "Error: Property value (%ld) out of range for property %s.\n\n", tmpl, propname); fprintf(stderr, "Error: Property value (%lld) out of range for property %s.\n\n", tmpll, propname);
return MOSQ_ERR_INVAL; 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; break;
case MQTT_PROP_TYPE_VARINT: case MQTT_PROP_TYPE_VARINT:
tmpl = atol(value); tmpll = atoll(value);
if(tmpl < 0 || tmpl > UINT32_MAX){ if(tmpll < 0 || tmpll > UINT32_MAX){
fprintf(stderr, "Error: Property value (%ld) out of range for property %s.\n\n", tmpl, propname); fprintf(stderr, "Error: Property value (%lld) out of range for property %s.\n\n", tmpll, propname);
return MOSQ_ERR_INVAL; 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; break;
case MQTT_PROP_TYPE_BINARY: case MQTT_PROP_TYPE_BINARY:
szt = strlen(value); szt = strlen(value);

View file

@ -226,7 +226,7 @@ static void init_config(struct mosq_config *cfg, int pub_or_sub)
}else{ }else{
cfg->protocol_version = MQTT_PROTOCOL_V311; 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; 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 #endif
if(cfg->protocol_version == 5){ if(cfg->protocol_version == 5){
if(cfg->clean_session == false && cfg->session_expiry_interval == -1){ if(cfg->session_expiry_interval == -2){
/* User hasn't set session-expiry-interval, but has cleared clean if(cfg->clean_session == false){
* session so default to persistent session. */ /* User hasn't set session-expiry-interval, but has cleared clean
cfg->session_expiry_interval = UINT32_MAX; * 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 > 0){
if(cfg->session_expiry_interval == UINT32_MAX && (cfg->id_prefix || !cfg->id)){ 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; cfg->session_expiry_interval = UINT32_MAX;
}else{ }else{
char *endptr = NULL; 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'){ if(endptr == argv[i+1] || endptr[0] != '\0'){
/* Entirety of argument wasn't a number */ /* Entirety of argument wasn't a number */
fprintf(stderr, "Error: session-expiry-interval not a number.\n\n"); fprintf(stderr, "Error: session-expiry-interval not a number.\n\n");
return 1; 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"); fprintf(stderr, "Error: session-expiry-interval out of range.\n\n");
return 1; return 1;
} }
if(cfg->session_expiry_interval == -1){ if(llvalue == -1LL){
/* Convenience value for infinity. */ /* Convenience value for infinity. */
cfg->session_expiry_interval = UINT32_MAX; cfg->session_expiry_interval = UINT32_MAX;
}else{
cfg->session_expiry_interval = llvalue;
} }
} }
cfg->protocol_version = MQTT_PROTOCOL_V5; cfg->protocol_version = MQTT_PROTOCOL_V5;

View file

@ -119,7 +119,7 @@ struct mosq_config {
bool pretty; /* sub, rr */ bool pretty; /* sub, rr */
unsigned int timeout; /* sub */ unsigned int timeout; /* sub */
int sub_opts; /* sub */ int sub_opts; /* sub */
long session_expiry_interval; int64_t session_expiry_interval;
int random_filter; /* sub */ int random_filter; /* sub */
int transport; int transport;
#ifndef WIN32 #ifndef WIN32