Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix passing NULL pointer to printf as string. #522

Merged
merged 1 commit into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void lwm2m_data_encode_string(const char * string,
size_t len;
int res;

LOG_ARG("\"%s\"", string);
LOG_ARG("\"%s\"", STR_NULL2EMPTY(string));
if (string == NULL)
{
len = 0;
Expand Down Expand Up @@ -274,7 +274,7 @@ void lwm2m_data_encode_nstring(const char * string,
size_t length,
lwm2m_data_t * dataP)
{
LOG_ARG("length: %d, string: \"%.*s\"", length, length, string);
LOG_ARG("length: %d, string: \"%.*s\"", length, length, STR_NULL2EMPTY(string));
lwm2m_data_encode_opaque((uint8_t *)string, length, dataP);

if (dataP->type == LWM2M_TYPE_OPAQUE)
Expand Down Expand Up @@ -584,7 +584,7 @@ int lwm2m_data_decode_bool(const lwm2m_data_t * dataP,

void lwm2m_data_encode_corelink(const char * corelink, lwm2m_data_t * dataP)
{
LOG_ARG("\"%s\"", corelink);
LOG_ARG("\"%s\"", STR_NULL2EMPTY(corelink));
lwm2m_data_encode_string(corelink, dataP);
if (dataP->type == LWM2M_TYPE_STRING)
{
Expand Down
1 change: 1 addition & 0 deletions core/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
((S) == STATE_REGISTERING ? "STATE_REGISTERING" : \
((S) == STATE_READY ? "STATE_READY" : \
"Unknown"))))))
#define STR_NULL2EMPTY(S) ((const char *)(S) ? (const char *)(S) : "")
#else
#define LOG_ARG(FMT, ...)
#define LOG(STR)
Expand Down
2 changes: 1 addition & 1 deletion core/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ int json_parse(lwm2m_uri_t * uriP,
_record_t * recordArray;
lwm2m_data_t * parsedP;

LOG_ARG("bufferLen: %d, buffer: \"%s\"", bufferLen, (char *)buffer);
LOG_ARG("bufferLen: %d, buffer: \"%s\"", bufferLen, STR_NULL2EMPTY((char *)buffer));
LOG_URI(uriP);
*dataP = NULL;
recordArray = NULL;
Expand Down
6 changes: 5 additions & 1 deletion core/liblwm2m.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,11 @@ int lwm2m_configure(lwm2m_context_t * contextP,
int i;
uint8_t found;

LOG_ARG("endpointName: \"%s\", msisdn: \"%s\", altPath: \"%s\", numObject: %d", endpointName, msisdn, altPath, numObject);
LOG_ARG("endpointName: \"%s\", msisdn: \"%s\", altPath: \"%s\", numObject: %d",
STR_NULL2EMPTY(endpointName),
STR_NULL2EMPTY(msisdn),
STR_NULL2EMPTY(altPath),
numObject);
// This API can be called only once for now
if (contextP->endpointName != NULL || contextP->objectList != NULL) return COAP_400_BAD_REQUEST;

Expand Down
7 changes: 6 additions & 1 deletion core/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,12 @@ uint8_t dm_handleRequest(lwm2m_context_t * contextP,
else
{
length = (size_t)res;
LOG_ARG("Observe Request[/%d/%d/%d]: %.*s\n", uriP->objectId, uriP->instanceId, uriP->resourceId, length, buffer);
LOG_ARG("Observe Request[/%d/%d/%d]: %.*s\n",
uriP->objectId,
uriP->instanceId,
uriP->resourceId,
length,
STR_NULL2EMPTY(buffer));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static uint8_t handle_request(lwm2m_context_t * contextP,
uint8_t result = COAP_IGNORE;

LOG("Entering");

#ifdef LWM2M_CLIENT_MODE
requestType = uri_decode(contextP->altPath, message->uri_path, &uri);
#else
Expand Down Expand Up @@ -213,7 +213,7 @@ void lwm2m_handle_packet(lwm2m_context_t * contextP,
{
LOG_ARG("Parsed: ver %u, type %u, tkl %u, code %u.%.2u, mid %u, Content type: %d",
message->version, message->type, message->token_len, message->code >> 5, message->code & 0x1F, message->mid, message->content_type);
LOG_ARG("Payload: %.*s", message->payload_len, message->payload);
LOG_ARG("Payload: %.*s", message->payload_len, STR_NULL2EMPTY(message->payload));
if (message->code >= COAP_GET && message->code <= COAP_DELETE)
{
uint32_t block_num = 0;
Expand Down Expand Up @@ -400,7 +400,7 @@ void lwm2m_handle_packet(lwm2m_context_t * contextP,

if (coap_error_code != NO_ERROR && coap_error_code != COAP_IGNORE)
{
LOG_ARG("ERROR %u: %s", coap_error_code, coap_error_message);
LOG_ARG("ERROR %u: %s", coap_error_code, STR_NULL2EMPTY(coap_error_message));

/* Set to sendable error code. */
if (coap_error_code >= 192)
Expand Down
2 changes: 1 addition & 1 deletion core/senml_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ int senml_json_parse(const lwm2m_uri_t * uriP,
time_t baseTime;
lwm2m_data_t baseValue;

LOG_ARG("bufferLen: %d, buffer: \"%s\"", bufferLen, (char *)buffer);
LOG_ARG("bufferLen: %d, buffer: \"%s\"", bufferLen, STR_NULL2EMPTY((char *)buffer));
LOG_URI(uriP);
*dataP = NULL;
recordArray = NULL;
Expand Down
28 changes: 14 additions & 14 deletions core/transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ lwm2m_transaction_t * transaction_new(void * sessionH,
int result;

LOG_ARG("method: %d, altPath: \"%s\", mID: %d, token_len: %d",
method, altPath, mID, token_len);
method, STR_NULL2EMPTY(altPath), mID, token_len);
LOG_URI(uriP);

// no transactions without peer
Expand Down Expand Up @@ -285,11 +285,11 @@ bool transaction_handleResponse(lwm2m_context_t * contextP,
if ((COAP_TYPE_ACK == message->type) || (COAP_TYPE_RST == message->type))
{
if (transacP->mID == message->mid)
{
found = true;
transacP->ack_received = true;
reset = COAP_TYPE_RST == message->type;
}
{
found = true;
transacP->ack_received = true;
reset = COAP_TYPE_RST == message->type;
}
}
}

Expand All @@ -305,14 +305,14 @@ bool transaction_handleResponse(lwm2m_context_t * contextP,
coap_init_message(response, COAP_TYPE_ACK, 0, message->mid);
message_send(contextP, response, fromSessionH);
}
if ((COAP_401_UNAUTHORIZED == message->code) && (COAP_MAX_RETRANSMIT > transacP->retrans_counter))
{
transacP->ack_received = false;
transacP->retrans_time += COAP_RESPONSE_TIMEOUT;
return true;
}
}

if ((COAP_401_UNAUTHORIZED == message->code) && (COAP_MAX_RETRANSMIT > transacP->retrans_counter))
{
transacP->ack_received = false;
transacP->retrans_time += COAP_RESPONSE_TIMEOUT;
return true;
}
}
if (transacP->callback != NULL)
{
transacP->callback(contextP, transacP, message);
Expand Down
4 changes: 2 additions & 2 deletions core/uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ lwm2m_request_type_t uri_decode(char * altPath,
int readNum;
lwm2m_request_type_t requestType = LWM2M_REQUEST_TYPE_DM;

LOG_ARG("altPath: \"%s\"", altPath);
LOG_ARG("altPath: \"%s\"", STR_NULL2EMPTY(altPath));

LWM2M_URI_RESET(uriP);

Expand Down Expand Up @@ -216,7 +216,7 @@ int lwm2m_stringToUri(const char * buffer,
size_t head;
int readNum;

LOG_ARG("buffer_len: %u, buffer: \"%.*s\"", buffer_len, buffer_len, buffer);
LOG_ARG("buffer_len: %u, buffer: \"%.*s\"", buffer_len, buffer_len, STR_NULL2EMPTY(buffer));

if (uriP == NULL) return 0;

Expand Down