Const-fy char pointers that point to .rodata strings

Using modifiable char pointers to access read-only strings may
lead to invalid memory access.
Signed-off-by: 's avatarGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
parent 3768362a
...@@ -69,7 +69,7 @@ typedef struct ...@@ -69,7 +69,7 @@ typedef struct
} storageElement; } storageElement;
static Tree heap; /**< Tree that holds the allocation records */ static Tree heap; /**< Tree that holds the allocation records */
static char* errmsg = "Memory allocation error"; static const char *errmsg = "Memory allocation error";
size_t Heap_roundup(size_t size); size_t Heap_roundup(size_t size);
...@@ -197,7 +197,7 @@ void checkEyecatchers(char* file, int line, void* p, size_t size) ...@@ -197,7 +197,7 @@ void checkEyecatchers(char* file, int line, void* p, size_t size)
int *sp = (int*)p; int *sp = (int*)p;
char *cp = (char*)p; char *cp = (char*)p;
int us; int us;
static char* msg = "Invalid %s eyecatcher %d in heap item at file %s line %d"; static const char *msg = "Invalid %s eyecatcher %d in heap item at file %s line %d";
if ((us = *--sp) != eyecatcher) if ((us = *--sp) != eyecatcher)
Log(LOG_ERROR, 13, msg, "start", us, file, line); Log(LOG_ERROR, 13, msg, "start", us, file, line);
......
...@@ -105,12 +105,12 @@ static enum LOG_LEVELS trace_output_level = INVALID_LEVEL; ...@@ -105,12 +105,12 @@ static enum LOG_LEVELS trace_output_level = INVALID_LEVEL;
static Log_traceCallback* trace_callback = NULL; static Log_traceCallback* trace_callback = NULL;
static traceEntry* Log_pretrace(void); static traceEntry* Log_pretrace(void);
static char* Log_formatTraceEntry(traceEntry* cur_entry); static char* Log_formatTraceEntry(traceEntry* cur_entry);
static void Log_output(enum LOG_LEVELS log_level, char* msg); static void Log_output(enum LOG_LEVELS log_level, const char *msg);
static void Log_posttrace(enum LOG_LEVELS log_level, traceEntry* cur_entry); static void Log_posttrace(enum LOG_LEVELS log_level, traceEntry* cur_entry);
static void Log_trace(enum LOG_LEVELS log_level, char* buf); static void Log_trace(enum LOG_LEVELS log_level, const char *buf);
#if 0 #if 0
FILE* Log_destToFile(char* dest); FILE* Log_destToFile(const char *dest);
int Log_compareEntries(char* entry1, char* entry2); int Log_compareEntries(const char *entry1, const char *entry2);
#endif #endif
static int sametime_count = 0; static int sametime_count = 0;
...@@ -316,7 +316,7 @@ static char* Log_formatTraceEntry(traceEntry* cur_entry) ...@@ -316,7 +316,7 @@ static char* Log_formatTraceEntry(traceEntry* cur_entry)
strncpy(&msg_buf[buf_pos], cur_entry->name, sizeof(msg_buf)-buf_pos); strncpy(&msg_buf[buf_pos], cur_entry->name, sizeof(msg_buf)-buf_pos);
else else
{ {
char* format = Messages_get(cur_entry->number, cur_entry->level); const char *format = Messages_get(cur_entry->number, cur_entry->level);
if (cur_entry->has_rc == 1) if (cur_entry->has_rc == 1)
snprintf(&msg_buf[buf_pos], sizeof(msg_buf)-buf_pos, format, cur_entry->thread_id, snprintf(&msg_buf[buf_pos], sizeof(msg_buf)-buf_pos, format, cur_entry->thread_id,
cur_entry->depth, "", cur_entry->depth, cur_entry->name, cur_entry->line, cur_entry->rc); cur_entry->depth, "", cur_entry->depth, cur_entry->name, cur_entry->line, cur_entry->rc);
...@@ -328,7 +328,7 @@ static char* Log_formatTraceEntry(traceEntry* cur_entry) ...@@ -328,7 +328,7 @@ static char* Log_formatTraceEntry(traceEntry* cur_entry)
} }
static void Log_output(enum LOG_LEVELS log_level, char* msg) static void Log_output(enum LOG_LEVELS log_level, const char *msg)
{ {
if (trace_destination) if (trace_destination)
{ {
...@@ -368,7 +368,7 @@ static void Log_posttrace(enum LOG_LEVELS log_level, traceEntry* cur_entry) ...@@ -368,7 +368,7 @@ static void Log_posttrace(enum LOG_LEVELS log_level, traceEntry* cur_entry)
} }
static void Log_trace(enum LOG_LEVELS log_level, char* buf) static void Log_trace(enum LOG_LEVELS log_level, const char *buf)
{ {
traceEntry *cur_entry = NULL; traceEntry *cur_entry = NULL;
...@@ -397,11 +397,11 @@ static void Log_trace(enum LOG_LEVELS log_level, char* buf) ...@@ -397,11 +397,11 @@ static void Log_trace(enum LOG_LEVELS log_level, char* buf)
* @param aFormat the printf format string to be used if the message id does not exist * @param aFormat the printf format string to be used if the message id does not exist
* @param ... the printf inserts * @param ... the printf inserts
*/ */
void Log(enum LOG_LEVELS log_level, int msgno, char* format, ...) void Log(enum LOG_LEVELS log_level, int msgno, const char *format, ...)
{ {
if (log_level >= trace_settings.trace_level) if (log_level >= trace_settings.trace_level)
{ {
char* temp = NULL; const char *temp = NULL;
static char msg_buf[512]; static char msg_buf[512];
va_list args; va_list args;
...@@ -471,7 +471,7 @@ void Log_stackTrace(enum LOG_LEVELS log_level, int msgno, int thread_id, int cur ...@@ -471,7 +471,7 @@ void Log_stackTrace(enum LOG_LEVELS log_level, int msgno, int thread_id, int cur
#if 0 #if 0
FILE* Log_destToFile(char* dest) FILE* Log_destToFile(const char *dest)
{ {
FILE* file = NULL; FILE* file = NULL;
...@@ -490,7 +490,7 @@ FILE* Log_destToFile(char* dest) ...@@ -490,7 +490,7 @@ FILE* Log_destToFile(char* dest)
} }
int Log_compareEntries(char* entry1, char* entry2) int Log_compareEntries(const char *entry1, const char *entry2)
{ {
int comp = strncmp(&entry1[7], &entry2[7], 19); int comp = strncmp(&entry1[7], &entry2[7], 19);
......
...@@ -75,10 +75,10 @@ typedef struct ...@@ -75,10 +75,10 @@ typedef struct
int Log_initialize(Log_nameValue*); int Log_initialize(Log_nameValue*);
void Log_terminate(void); void Log_terminate(void);
void Log(enum LOG_LEVELS, int, char *, ...); void Log(enum LOG_LEVELS, int, const char *, ...);
void Log_stackTrace(enum LOG_LEVELS, int, int, int, const char*, int, int*); void Log_stackTrace(enum LOG_LEVELS, int, int, int, const char*, int, int*);
typedef void Log_traceCallback(enum LOG_LEVELS level, char* message); typedef void Log_traceCallback(enum LOG_LEVELS level, const char *message);
void Log_setTraceCallback(Log_traceCallback* callback); void Log_setTraceCallback(Log_traceCallback* callback);
void Log_setTraceLevel(enum LOG_LEVELS level); void Log_setTraceLevel(enum LOG_LEVELS level);
......
...@@ -61,8 +61,8 @@ ...@@ -61,8 +61,8 @@
#include "VersionInfo.h" #include "VersionInfo.h"
char* client_timestamp_eye = "MQTTAsyncV3_Timestamp " BUILD_TIMESTAMP; const char *client_timestamp_eye = "MQTTAsyncV3_Timestamp " BUILD_TIMESTAMP;
char* client_version_eye = "MQTTAsyncV3_Version " CLIENT_VERSION; const char *client_version_eye = "MQTTAsyncV3_Version " CLIENT_VERSION;
#if !defined(min) #if !defined(min)
#define min(a, b) (((a) < (b)) ? (a) : (b)) #define min(a, b) (((a) < (b)) ? (a) : (b))
......
...@@ -347,7 +347,7 @@ typedef struct ...@@ -347,7 +347,7 @@ typedef struct
/** A numeric code identifying the error. */ /** A numeric code identifying the error. */
int code; int code;
/** Optional text explaining the error. Can be NULL. */ /** Optional text explaining the error. Can be NULL. */
char* message; const char *message;
} MQTTAsync_failureData; } MQTTAsync_failureData;
/** The data returned on completion of a successful API call in the response callback onSuccess. */ /** The data returned on completion of a successful API call in the response callback onSuccess. */
......
...@@ -65,8 +65,8 @@ ...@@ -65,8 +65,8 @@
#include "VersionInfo.h" #include "VersionInfo.h"
char* client_timestamp_eye = "MQTTClientV3_Timestamp " BUILD_TIMESTAMP; const char *client_timestamp_eye = "MQTTClientV3_Timestamp " BUILD_TIMESTAMP;
char* client_version_eye = "MQTTClientV3_Version " CLIENT_VERSION; const char *client_version_eye = "MQTTClientV3_Version " CLIENT_VERSION;
static ClientStates ClientState = static ClientStates ClientState =
{ {
......
...@@ -43,14 +43,14 @@ ...@@ -43,14 +43,14 @@
/** /**
* List of the predefined MQTT v3 packet names. * List of the predefined MQTT v3 packet names.
*/ */
static char* packet_names[] = static const char *packet_names[] =
{ {
"RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL", "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
"PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK", "PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
"PINGREQ", "PINGRESP", "DISCONNECT" "PINGREQ", "PINGRESP", "DISCONNECT"
}; };
char** MQTTClient_packet_names = packet_names; const char** MQTTClient_packet_names = packet_names;
/** /**
...@@ -58,7 +58,7 @@ char** MQTTClient_packet_names = packet_names; ...@@ -58,7 +58,7 @@ char** MQTTClient_packet_names = packet_names;
* @param ptype packet code * @param ptype packet code
* @return the corresponding string, or "UNKNOWN" * @return the corresponding string, or "UNKNOWN"
*/ */
char* MQTTPacket_name(int ptype) const char* MQTTPacket_name(int ptype)
{ {
return (ptype >= 0 && ptype <= DISCONNECT) ? packet_names[ptype] : "UNKNOWN"; return (ptype >= 0 && ptype <= DISCONNECT) ? packet_names[ptype] : "UNKNOWN";
} }
......
...@@ -225,7 +225,7 @@ void writeChar(char** pptr, char c); ...@@ -225,7 +225,7 @@ void writeChar(char** pptr, char c);
void writeInt(char** pptr, int anInt); void writeInt(char** pptr, int anInt);
void writeUTF(char** pptr, const char* string); void writeUTF(char** pptr, const char* string);
char* MQTTPacket_name(int ptype); const char* MQTTPacket_name(int ptype);
void* MQTTPacket_Factory(networkHandles* net, int* error); void* MQTTPacket_Factory(networkHandles* net, int* error);
int MQTTPacket_send(networkHandles* net, Header header, char* buffer, size_t buflen, int free); int MQTTPacket_send(networkHandles* net, Header header, char* buffer, size_t buflen, int free);
......
...@@ -718,8 +718,8 @@ int main (int argc, char *argv[]) ...@@ -718,8 +718,8 @@ int main (int argc, char *argv[])
int rc; int rc;
char *handle; char *handle;
char *perdir = "."; char *perdir = ".";
char *clientID = "TheUTClient"; const char *clientID = "TheUTClient";
char *serverURI = "127.0.0.1:1883"; const char *serverURI = "127.0.0.1:1883";
char *stem = MSTEM; char *stem = MSTEM;
int msgId, i; int msgId, i;
......
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
#define max_msg_len 120 #define max_msg_len 120
static char* protocol_message_list[] = static const char *protocol_message_list[] =
{ {
"%d %s -> CONNECT cleansession: %d (%d)", /* 0, was 131, 68 and 69 */ "%d %s -> CONNECT cleansession: %d (%d)", /* 0, was 131, 68 and 69 */
"%d %s <- CONNACK rc: %d", /* 1, was 132 */ "%d %s <- CONNACK rc: %d", /* 1, was 132 */
...@@ -69,7 +69,7 @@ static char* protocol_message_list[] = ...@@ -69,7 +69,7 @@ static char* protocol_message_list[] =
"Socket error for client identifier %s, socket %d, peer address %s; ending connection", /* 29 */ "Socket error for client identifier %s, socket %d, peer address %s; ending connection", /* 29 */
}; };
static char* trace_message_list[] = static const char *trace_message_list[] =
{ {
"Failed to remove client from bstate->clients", /* 0 */ "Failed to remove client from bstate->clients", /* 0 */
"Removed client %s from bstate->clients, socket %d", /* 1 */ "Removed client %s from bstate->clients, socket %d", /* 1 */
...@@ -92,9 +92,9 @@ static char* trace_message_list[] = ...@@ -92,9 +92,9 @@ static char* trace_message_list[] =
* @param log_level the log level, used to determine which message list to use * @param log_level the log level, used to determine which message list to use
* @return the message format string * @return the message format string
*/ */
char* Messages_get(int index, enum LOG_LEVELS log_level) const char* Messages_get(int index, enum LOG_LEVELS log_level)
{ {
char* msg = NULL; const char *msg = NULL;
if (log_level == TRACE_PROTOCOL) if (log_level == TRACE_PROTOCOL)
msg = (index >= 0 && index < ARRAY_SIZE(protocol_message_list)) ? protocol_message_list[index] : NULL; msg = (index >= 0 && index < ARRAY_SIZE(protocol_message_list)) ? protocol_message_list[index] : NULL;
......
...@@ -19,6 +19,6 @@ ...@@ -19,6 +19,6 @@
#include "Log.h" #include "Log.h"
char* Messages_get(int, enum LOG_LEVELS); const char* Messages_get(int, enum LOG_LEVELS);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment