Skip to content

Commit 32119c1

Browse files
committed
Prevent crashes, verify parse is not null before use
1 parent d108461 commit 32119c1

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

src/SparkFun_Extensible_Message_Parser.cpp

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,20 @@ const char * sempGetTypeName(SEMP_PARSE_STATE *parse, uint16_t type)
113113
{
114114
const char *name = "Unknown parser";
115115

116-
if (type == parse->parserCount)
117-
name = "No active parser, scanning for preamble";
118-
else if (parse->parserNames && (type < parse->parserCount))
119-
name = parse->parserNames[type];
116+
if (parse)
117+
{
118+
if (type == parse->parserCount)
119+
name = "No active parser, scanning for preamble";
120+
else if (parse->parserNames && (type < parse->parserCount))
121+
name = parse->parserNames[type];
122+
}
120123
return name;
121124
}
122125

123126
// Print the parser's configuration
124127
void sempPrintParserConfiguration(SEMP_PARSE_STATE *parse, Print *print)
125128
{
126-
if (print)
129+
if (print && parse)
127130
{
128131
sempPrintln(print, "SparkFun Extensible Message Parser");
129132
sempPrintf(print, " Name: %p (%s)", parse->parserName, parse->parserName);
@@ -183,33 +186,37 @@ void sempPrintln(Print *print, const char *string)
183186
// Translates state value into an ASCII state name
184187
const char * sempGetStateName(const SEMP_PARSE_STATE *parse)
185188
{
186-
if (parse->state == sempFirstByte)
189+
if (parse && (parse->state == sempFirstByte))
187190
return "sempFirstByte";
188191
return "Unknown state";
189192
}
190193

191194
// Disable debug output
192195
void sempDisableDebugOutput(SEMP_PARSE_STATE *parse)
193196
{
194-
parse->printDebug = nullptr;
197+
if (parse)
198+
parse->printDebug = nullptr;
195199
}
196200

197201
// Enable debug output
198202
void sempEnableDebugOutput(SEMP_PARSE_STATE *parse, Print *print)
199203
{
200-
parse->printDebug = print;
204+
if (parse)
205+
parse->printDebug = print;
201206
}
202207

203208
// Disable error output
204209
void sempDisableErrorOutput(SEMP_PARSE_STATE *parse)
205210
{
206-
parse->printError = nullptr;
211+
if (parse)
212+
parse->printError = nullptr;
207213
}
208214

209215
// Enable error output
210216
void sempEnableErrorOutput(SEMP_PARSE_STATE *parse, Print *print)
211217
{
212-
parse->printError = print;
218+
if (parse)
219+
parse->printError = print;
213220
}
214221

215222
//----------------------------------------
@@ -309,54 +316,60 @@ bool sempFirstByte(SEMP_PARSE_STATE *parse, uint8_t data)
309316
int index;
310317
SEMP_PARSE_ROUTINE parseRoutine;
311318

312-
// Add this byte to the buffer
313-
parse->crc = 0;
314-
parse->computeCrc = nullptr;
315-
parse->length = 0;
316-
parse->type = parse->parserCount;
317-
parse->buffer[parse->length++] = data;
318-
319-
// Walk through the parse table
320-
for (index = 0; index < parse->parserCount; index++)
319+
if (parse)
321320
{
322-
parseRoutine = parse->parsers[index];
323-
if (parseRoutine(parse, data))
321+
// Add this byte to the buffer
322+
parse->crc = 0;
323+
parse->computeCrc = nullptr;
324+
parse->length = 0;
325+
parse->type = parse->parserCount;
326+
parse->buffer[parse->length++] = data;
327+
328+
// Walk through the parse table
329+
for (index = 0; index < parse->parserCount; index++)
324330
{
325-
parse->type = index;
326-
return true;
331+
parseRoutine = parse->parsers[index];
332+
if (parseRoutine(parse, data))
333+
{
334+
parse->type = index;
335+
return true;
336+
}
327337
}
328-
}
329338

330-
// Preamble byte not found, continue searching for a preamble byte
331-
parse->state = sempFirstByte;
339+
// Preamble byte not found, continue searching for a preamble byte
340+
parse->state = sempFirstByte;
341+
}
332342
return false;
333343
}
334344

335345
// Parse the next byte
336346
void sempParseNextByte(SEMP_PARSE_STATE *parse, uint8_t data)
337347
{
338-
// Verify that enough space exists in the buffer
339-
if (parse->length >= parse->bufferLength)
348+
if (parse)
340349
{
341-
// Message too long
342-
sempPrintf(parse->printError, "SEMP %s NMEA: Message too long, increase the buffer size > %d",
343-
parse->parserName,
344-
parse->bufferLength);
345-
346-
// Start searching for a preamble byte
347-
sempFirstByte(parse, data);
348-
return;
349-
}
350+
// Verify that enough space exists in the buffer
351+
if (parse->length >= parse->bufferLength)
352+
{
353+
// Message too long
354+
sempPrintf(parse->printError, "SEMP %s NMEA: Message too long, increase the buffer size > %d",
355+
parse->parserName,
356+
parse->bufferLength);
357+
358+
// Start searching for a preamble byte
359+
sempFirstByte(parse, data);
360+
return;
361+
}
350362

351-
// Save the data byte
352-
parse->buffer[parse->length++] = data;
363+
// Save the data byte
364+
parse->buffer[parse->length++] = data;
353365

354-
// Compute the CRC value for the message
355-
if (parse->computeCrc)
356-
parse->crc = parse->computeCrc(parse, data);
366+
// Compute the CRC value for the message
367+
if (parse->computeCrc)
368+
parse->crc = parse->computeCrc(parse, data);
357369

358-
// Update the parser state based on the incoming byte
359-
parse->state(parse, data);
370+
// Update the parser state based on the incoming byte
371+
parse->state(parse, data);
372+
}
360373
}
361374

362375
// Shutdown the parser

0 commit comments

Comments
 (0)