Bug Summary

File:builds/wireshark/wireshark/wiretap/cllog.c
Warning:line 470, column 33
Addition of a null pointer (from variable 'pFieldEnd') and a nonzero integer value results in undefined behavior

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name cllog.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /usr/include/libxml2 -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -D wiretap_EXPORTS -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/wiretap -I /builds/wireshark/wireshark/build/wiretap -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-12-03-100333-3661-1 -x c /builds/wireshark/wireshark/wiretap/cllog.c
1/* cllog.c
2 *
3 * Wiretap Library
4 * Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9/*
10 * Reads log files from CLX000 CAN loggers from CSS Electronics:
11 *
12 * https://canlogger.csselectronics.com/clx000-docs/cl1000/log/index.html
13 * https://canlogger.csselectronics.com/clx000-docs/cl2000/log/index.html
14 *
15 * Based on the cCLLog.c, cCLLog.h, and wtap-cllog.c source files from
16 * the WS_v2.4-Plugin_v7.1.zip version of the CSS Electronics plugin at
17 *
18 * https://canlogger.csselectronics.com/downloads.php?q=wireshark
19 *
20 * with the files combined into one source file, modernized to
21 * fit into an up-to-date version of Wireshark, and cleaned up
22 * not to, for example, do seeks by rewinding and reading to
23 * get to the seek target.
24 *
25 * It could probably use some further cleanup.
26 */
27
28#include "config.h"
29
30#include <stdio.h>
31#include <stdbool.h>
32#include <stdint.h>
33#include <string.h>
34#include <stdlib.h>
35#include <time.h>
36
37#include <wsutil/array.h>
38#include <wsutil/str_util.h>
39
40#include "socketcan.h"
41#include "file_wrappers.h"
42#include "cllog.h"
43
44static int cllog_file_type_subtype = -1;
45
46/***********************************************************************************************************************
47 * Public definitions
48 **********************************************************************************************************************/
49#define MAX_LOG_LINE_FIELDS7 7 /*( seqNo, timestamp, lost, SE, ID, length, data) */
50/***********************************************************************************************************************
51 * Public type declarations
52 **********************************************************************************************************************/
53
54 /* Message type */
55typedef enum
56{
57 msg_rx_standard_e = 0,
58 msg_rx_extended_e = 1,
59 msg_tx_standard_e = 7,
60 msg_tx_extended_e = 8,
61} cCLLog_messageType_t;
62
63/* Typedef CAN-bus message type */
64typedef struct
65{
66 uint32_t lost;
67 cCLLog_messageType_t msgType;
68 wtap_can_msg_t msg;
69} cCLLog_message_t;
70
71/* Silent-mode*/
72typedef enum { silent_disabled_e = 0, silent_enabled_e } cCLLog_silentMode_t;
73
74/* Cyclic-mode*/
75typedef enum { cyclic_disabled_e = 0, cyclic_enabled_e } cCLLog_cyclicMode_t;
76
77/* Logger type */
78typedef enum { type_CL1000_e = 0, type_CL2000_e, type_CL3000_e } cCLLog_loggerType_t;
79
80typedef char * (*CLLog_gets_t)(char *s, int size, void *stream);
81typedef int (*CLLog_rewind_t)(void *stream);
82
83typedef struct cLLog_private cCLLog_logFileInfo_t;
84
85/* Type used to parse a field in a log line */
86typedef bool_Bool (*parseFieldFunc_t)(cCLLog_logFileInfo_t *pInfo, char *pField, cCLLog_message_t *pLogEntry, int *err, char **err_info);
87
88/* Log file information */
89struct cLLog_private
90{
91 uint32_t firstLogRow;
92 cCLLog_loggerType_t loggerType;
93 char hwrev[5];
94 char fwrev[5];
95 char id[20];
96 uint32_t sessionNo;
97 uint32_t splitNo;
98 nstime_t logStartTime;
99 char logStartTimeString[ 20 ];
100 char separator;
101 uint8_t timeFormat;
102 char timeSeparator;
103 char timeSeparatorMs;
104 char dateSeparator;
105 char dateAndTimeSeparator;
106 uint32_t bitRate;
107 cCLLog_silentMode_t silentMode;
108 cCLLog_cyclicMode_t cyclicMode;
109
110 parseFieldFunc_t parseFieldFunc[ MAX_LOG_LINE_FIELDS7 ];
111
112 /* First log time stamp as relative offset */
113 nstime_t firstTimeStampAbs;
114};
115
116/***********************************************************************************************************************
117 * Private definitions
118 **********************************************************************************************************************/
119#define HEADER_LINE_PARSE_MAPPING_LENGTH(sizeof (headerLineParseMapping) / sizeof (headerLineParseMapping
)[0])
array_length(headerLineParseMapping)(sizeof (headerLineParseMapping) / sizeof (headerLineParseMapping
)[0])
120#define MAX_LOG_LINE_LENGTH200 200
121#define TIME_STAMP_STRING_MAX_LENGTH( sizeof( "YYYY/MM/DDThh:mm:ss.kkk" ) ) ( sizeof( "YYYY/MM/DDThh:mm:ss.kkk" ) )
122#define TIME_STAMP_STRING_STRIPPED_MAX_LENGTH( sizeof( "YYYYMMDDhhmmsskkk" ) ) ( sizeof( "YYYYMMDDhhmmsskkk" ) )
123
124/***********************************************************************************************************************
125 * Private type definitions
126 **********************************************************************************************************************/
127/* Function type to parse a single log file line */
128typedef bool_Bool (*parseFunc_t)(cCLLog_logFileInfo_t *pInfo, char *pLine, int *err, char **err_info);
129
130/* Structure of the header parse mapping. A match string is paired with a parse function */
131typedef struct
132{
133 const char *pMatchString;
134 parseFunc_t parseFunc;
135} headerLineParseMapping_t;
136
137/***********************************************************************************************************************
138 * Private function declarations
139 **********************************************************************************************************************/
140static bool_Bool parseColumnHeaderFields( cCLLog_logFileInfo_t *pInfo, char *pColLine );
141static uint8_t stripTimeStamp( const cCLLog_logFileInfo_t *pInfo, char *pTimeStampString );
142
143/* Parse header lines functions */
144static bool_Bool parseLogFileHeaderLine_type(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
145static bool_Bool parseLogFileHeaderLine_hwrev(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
146static bool_Bool parseLogFileHeaderLine_fwrev(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
147static bool_Bool parseLogFileHeaderLine_id(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
148static bool_Bool parseLogFileHeaderLine_sessionNo(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
149static bool_Bool parseLogFileHeaderLine_splitNo(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
150static bool_Bool parseLogFileHeaderLine_time(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
151static bool_Bool parseLogFileHeaderLine_valueSeparator(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
152static bool_Bool parseLogFileHeaderLine_timeFormat(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
153static bool_Bool parseLogFileHeaderLine_timeSeparator(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
154static bool_Bool parseLogFileHeaderLine_timeSeparatorMs(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
155static bool_Bool parseLogFileHeaderLine_dateSeparator(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
156static bool_Bool parseLogFileHeaderLine_timeAndDateSeparator(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
157static bool_Bool parseLogFileHeaderLine_bitRate(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
158static bool_Bool parseLogFileHeaderLine_silentMode(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
159static bool_Bool parseLogFileHeaderLine_cyclicMode(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info);
160/***********************************************************************************************************************
161 * Private variable definitions
162 **********************************************************************************************************************/
163
164/* Array of header line match strings and associated parse functions */
165static const headerLineParseMapping_t headerLineParseMapping[] =
166{
167 { .pMatchString = "Logger type: ", .parseFunc = parseLogFileHeaderLine_type},
168 { .pMatchString = "HW rev: ", .parseFunc = parseLogFileHeaderLine_hwrev },
169 { .pMatchString = "FW rev: ", .parseFunc = parseLogFileHeaderLine_fwrev },
170 { .pMatchString = "Logger ID: ", .parseFunc = parseLogFileHeaderLine_id},
171 { .pMatchString = "Session No.: ", .parseFunc = parseLogFileHeaderLine_sessionNo},
172 { .pMatchString = "Split No.: ", .parseFunc = parseLogFileHeaderLine_splitNo},
173 { .pMatchString = "Time: ", .parseFunc = parseLogFileHeaderLine_time},
174 { .pMatchString = "Value separator: ", .parseFunc = parseLogFileHeaderLine_valueSeparator},
175 { .pMatchString = "Time format: ", .parseFunc = parseLogFileHeaderLine_timeFormat},
176 { .pMatchString = "Time separator: ", .parseFunc = parseLogFileHeaderLine_timeSeparator},
177 { .pMatchString = "Time separator ms: ", .parseFunc = parseLogFileHeaderLine_timeSeparatorMs},
178 { .pMatchString = "Date separator: ", .parseFunc = parseLogFileHeaderLine_dateSeparator},
179 { .pMatchString = "Time and date separator: ", .parseFunc = parseLogFileHeaderLine_timeAndDateSeparator},
180 { .pMatchString = "Bit-rate: ", .parseFunc = parseLogFileHeaderLine_bitRate},
181 { .pMatchString = "Silent mode: ", .parseFunc = parseLogFileHeaderLine_silentMode},
182 { .pMatchString = "Cyclic mode: ", .parseFunc = parseLogFileHeaderLine_cyclicMode},
183};
184
185/*
186 * Do a string copy to a buffer of a specified length.
187 * If the string will fit, return true.
188 * If the string won't fit, return false.
189 */
190static bool_Bool
191checked_strcpy(char *dest, size_t destlen, const char *src)
192{
193 size_t srclen;
194
195 srclen = strlen(src) + 1; // count the trailing '\0'
196 if (srclen > destlen)
197 return false0;
198 memcpy(dest, src, srclen);
199 return true1;
200}
201
202/* TODO: Does not support separators set to numbers (will remove part of the time stamp also */
203/* TODO: Does not support time stamps without ms, as given in the header */
204/* TODO: Alot of copying slows down the parsing */
205static bool_Bool parseFieldTS(cCLLog_logFileInfo_t *pInfo, char *pField, cCLLog_message_t *pLogEntry, int *err, char **err_info)
206{
207 struct tm tm;
208 int ms;
209
210 /* Copy the string to not modify the original */
211 char timeStampCopy[TIME_STAMP_STRING_MAX_LENGTH( sizeof( "YYYY/MM/DDThh:mm:ss.kkk" ) )];
212 if (!checked_strcpy(timeStampCopy, sizeof timeStampCopy, pField))
213 {
214 *err = WTAP_ERR_BAD_FILE-13;
215 *err_info = g_strdup("cllog: time stamp is too long")g_strdup_inline ("cllog: time stamp is too long");
216 return false0;
217 }
218
219 /* Copy the header time stamp string to not modify the original */
220 char timeStampHeaderCopy[TIME_STAMP_STRING_MAX_LENGTH( sizeof( "YYYY/MM/DDThh:mm:ss.kkk" ) )];
221 if (!checked_strcpy(timeStampHeaderCopy, sizeof timeStampHeaderCopy, pInfo->logStartTimeString))
222 {
223 *err = WTAP_ERR_BAD_FILE-13;
224 *err_info = g_strdup("cllog: header time stamp too long")g_strdup_inline ("cllog: header time stamp too long");
225 return false0;
226 }
227
228 /* Strip the delimiters from the time strings */
229 uint8_t msgTimeStrippedLen = stripTimeStamp(pInfo, timeStampCopy);
230 if (msgTimeStrippedLen > TIME_STAMP_STRING_STRIPPED_MAX_LENGTH( sizeof( "YYYYMMDDhhmmsskkk" ) ) - 1) {
231 *err = WTAP_ERR_BAD_FILE-13;
232 *err_info = g_strdup("cllog: time stamp incorrectly formatted")g_strdup_inline ("cllog: time stamp incorrectly formatted");
233 return false0;
234 }
235
236 uint8_t headerTimeStrippedLen = stripTimeStamp(pInfo, timeStampHeaderCopy);
237 if (headerTimeStrippedLen > TIME_STAMP_STRING_STRIPPED_MAX_LENGTH( sizeof( "YYYYMMDDhhmmsskkk" ) ) - 1) {
238 *err = WTAP_ERR_BAD_FILE-13;
239 *err_info = g_strdup("cllog: header time stamp incorrectly formatted")g_strdup_inline ("cllog: header time stamp incorrectly formatted"
)
;
240 return false0;
241 }
242
243 /* Set time string (YYYYMMDDhhmmsskkk) to the epoch */
244 char timeStampStringFull[TIME_STAMP_STRING_STRIPPED_MAX_LENGTH( sizeof( "YYYYMMDDhhmmsskkk" ) )] = "19700101000000000";
245
246 /* Copy the header time to the template */
247 memcpy(timeStampStringFull, timeStampHeaderCopy, headerTimeStrippedLen);
248
249 /* Copy the stripped timestamp into the full template */
250 memcpy(&timeStampStringFull[TIME_STAMP_STRING_STRIPPED_MAX_LENGTH( sizeof( "YYYYMMDDhhmmsskkk" ) ) - 1 - msgTimeStrippedLen], timeStampCopy, msgTimeStrippedLen);
251 timeStampStringFull[TIME_STAMP_STRING_STRIPPED_MAX_LENGTH( sizeof( "YYYYMMDDhhmmsskkk" ) ) - 1] = '\0';
252
253 memset(&tm, 0, sizeof tm);
254
255 /* YYYYMMDDThhmmss */
256 if (sscanf(timeStampStringFull, "%4u%2u%2u%2u%2u%2u%3d",
257 &tm.tm_year,
258 &tm.tm_mon,
259 &tm.tm_mday,
260 &tm.tm_hour,
261 &tm.tm_min,
262 &tm.tm_sec,
263 &ms
264 ) != 7) {
265 *err = WTAP_ERR_BAD_FILE-13;
266 *err_info = g_strdup("cllog: time stamp is not valid")g_strdup_inline ("cllog: time stamp is not valid");
267 return false0;
268 }
269 tm.tm_mon -= 1;
270 tm.tm_year -= 1900;
271
272 /* To Epoch (mktime converts to epoch from local (!!!) timezone) */
273 pLogEntry->msg.ts.secs = mktime(&tm);
274 pLogEntry->msg.ts.nsecs = ms*1000 * 1000;
275
276 /* Is first time stamp ? */
277 if (pInfo->firstTimeStampAbs.secs == 0 && pInfo->firstTimeStampAbs.nsecs == 0)
278 pInfo->firstTimeStampAbs = pLogEntry->msg.ts;
279
280 return true1;
281}
282
283static bool_Bool parseFieldLost(cCLLog_logFileInfo_t *pInfo _U___attribute__((unused)), char *pField, cCLLog_message_t *pLogEntry, int *err, char **err_info)
284{
285 uint32_t lost;
286
287 if (!wtap_strtou32("cllog", "lost packet count", pField, &lost, err, err_info)) {
288 return false0;
289 }
290 pLogEntry->lost = lost;
291 return true1;
292}
293
294static bool_Bool parseFieldMsgType(cCLLog_logFileInfo_t *pInfo _U___attribute__((unused)), char *pField, cCLLog_message_t *pLogEntry, int *err, char **err_info)
295{
296 switch (pField[0])
297 {
298 case '0':
299 pLogEntry->msgType = msg_rx_standard_e;
300 pLogEntry->msg.type = MSG_TYPE_STD;
301 return true1;
302 case '1':
303 pLogEntry->msgType = msg_rx_extended_e;
304 pLogEntry->msg.type = MSG_TYPE_EXT;
305 return true1;
306 case '8':
307 pLogEntry->msgType = msg_tx_standard_e;
308 pLogEntry->msg.type = MSG_TYPE_STD;
309 return true1;
310 case '9':
311 pLogEntry->msgType = msg_tx_extended_e;
312 pLogEntry->msg.type = MSG_TYPE_EXT;
313 return true1;
314 default:
315 *err = WTAP_ERR_BAD_FILE-13;
316 *err_info = g_strdup("cllog: unknown message type")g_strdup_inline ("cllog: unknown message type");
317 return false0;
318 }
319}
320
321static bool_Bool parseFieldID(cCLLog_logFileInfo_t *pInfo _U___attribute__((unused)), char *pField, cCLLog_message_t *pLogEntry, int *err, char **err_info)
322{
323 uint32_t id;
324
325 if (!wtap_hexstrtou32("cllog", "ID value", pField, &id, err, err_info)) {
326 return false0;
327 }
328 pLogEntry->msg.id = id;
329 return true1;
330}
331
332static bool_Bool parseFieldLength(cCLLog_logFileInfo_t *pInfo _U___attribute__((unused)), char *pField, cCLLog_message_t *pLogEntry, int *err, char **err_info)
333{
334 uint32_t length;
335
336 if (!wtap_strtou32("cllog", "length", pField, &length, err, err_info)) {
337 return false0;
338 }
339 if (length > array_length(pLogEntry->msg.data.data)(sizeof (pLogEntry->msg.data.data) / sizeof (pLogEntry->
msg.data.data)[0])
) {
340 *err = WTAP_ERR_BAD_FILE-13;
341 *err_info = g_strdup_printf("cllog: length value %u > maximum length %zu",
342 length, array_length(pLogEntry->msg.data.data)(sizeof (pLogEntry->msg.data.data) / sizeof (pLogEntry->
msg.data.data)[0])
);
343 return false0;
344 }
345 pLogEntry->msg.data.length = length;
346 return true1;
347}
348
349static bool_Bool parseFieldData(cCLLog_logFileInfo_t *pInfo _U___attribute__((unused)), char *pField, cCLLog_message_t *pLogEntry, int *err, char **err_info)
350{
351 char *pFieldStart = pField;
352
353 /* Set data length in case length field is not set explicitly in the log file */
354 pLogEntry->msg.data.length = 0;
355
356 /* Loop all data bytes */
357 while (pLogEntry->msg.data.length < array_length(pLogEntry->msg.data.data)(sizeof (pLogEntry->msg.data.data) / sizeof (pLogEntry->
msg.data.data)[0])
)
358 {
359 int hexdigit;
360 uint8_t data;
361
362 if (*pFieldStart == '\n' || *pFieldStart == '\r' || *pFieldStart == '\0')
363 {
364 break;
365 }
366
367 hexdigit = ws_xton(*pFieldStart);
368 if (hexdigit < 0) {
369 *err = WTAP_ERR_BAD_FILE-13;
370 *err_info = g_strdup_printf("cllog: packet byte value 0x%02x is not valid", *pFieldStart);
371 return false0;
372 }
373 data = (uint8_t)hexdigit << 4U;
374 pFieldStart++;
375 hexdigit = ws_xton(*pFieldStart);
376 if (hexdigit < 0) {
377 *err = WTAP_ERR_BAD_FILE-13;
378 *err_info = g_strdup_printf("cllog: packet byte value 0x%02x is not valid", *pFieldStart);
379 return false0;
380 }
381 data = data | (uint8_t)hexdigit;
382 pFieldStart++;
383 pLogEntry->msg.data.data[pLogEntry->msg.data.length++] = data;
384 }
385 return true1;
386}
387
388static bool_Bool parseLogLine(cCLLog_logFileInfo_t *pInfo, char *pLine, cCLLog_message_t *pLogEntry, int *err, char **err_info)
389{
390 char *pFieldStart = pLine;
391
392 /* Loop all fields in log line */
393 for (unsigned int fieldNo = 0, finalField = 0; fieldNo < MAX_LOG_LINE_FIELDS7 && finalField == 0; fieldNo++)
394 {
395 /* Find field end by separator */
396 char *pFieldEnd = strchr(pFieldStart, pInfo->separator);
397
398 /* If final field, then EOL marks the end of the field */
399 if (pFieldEnd == NULL((void*)0))
400 {
401 pFieldEnd = strchr(pFieldStart, '\n');
402 finalField = 1;
403 }
404
405 /* Replace separator or terminator with string termination */
406 if (pFieldEnd != NULL((void*)0))
407 {
408 *pFieldEnd = '\0';
409 }
410
411 /* Is parse function assigned to field? */
412 if (pInfo->parseFieldFunc[fieldNo] != NULL((void*)0))
413 {
414 /* Parse field */
415 if (!pInfo->parseFieldFunc[fieldNo](pInfo, pFieldStart, pLogEntry, err, err_info))
416 {
417 return false0;
418 }
419 }
420
421 /* Skip over the separator */
422 pFieldStart = pFieldEnd + 1;
423 }
424 return true1;
425}
426
427/***********************************************************************************************************************
428 * parseColumnHeaderFields
429 *
430 * Parse the column fields and determine which fields are present and the position of the fields
431 *
432 * @param[ in ] pInfo Pointer to the CLLog object
433 * @param[ in ] pColLine The column line
434 **********************************************************************************************************************/
435static bool_Bool parseColumnHeaderFields( cCLLog_logFileInfo_t *pInfo, char *pColLine )
436{
437 bool_Bool resultFlag = false0;
438
439 /* Initialise field start */
440 char *pFieldStart = pColLine;
441
442 /* Loop all fields in line */
443 for ( uint8_t fieldNo = 0, finalField = 0 ; fieldNo
0.1
'fieldNo' is < MAX_LOG_LINE_FIELDS
< MAX_LOG_LINE_FIELDS7 && finalField == 0 ; fieldNo++ )
1
Loop condition is true. Entering loop body
444 {
445 /* Find field end */
446 char *pFieldEnd = strchr( pFieldStart, pInfo->separator );
447
448 /* If final field, then EOL marks the end of the field */
449 if( pFieldEnd == NULL((void*)0) )
2
Assuming 'pFieldEnd' is equal to NULL
3
Taking true branch
450 {
451 pFieldEnd = strchr( pFieldStart, '\n' );
4
Value assigned to 'pFieldEnd'
452 finalField = 1;
453 }
454
455 /* Replace separator or terminator with string termination */
456 if (pFieldEnd != NULL((void*)0))
5
Assuming 'pFieldEnd' is equal to NULL
6
Taking false branch
457 {
458 *pFieldEnd = '\0';
459 }
460
461 /* Set field number */
462 if( strcmp( pFieldStart, "Timestamp" ) == 0 ) { pInfo->parseFieldFunc[ fieldNo ] = parseFieldTS; resultFlag = true1; }
7
Assuming the condition is false
8
Taking false branch
463 if( strcmp( pFieldStart, "Lost" ) == 0 ) { pInfo->parseFieldFunc[ fieldNo ] = parseFieldLost; resultFlag = true1; }
9
Assuming the condition is false
10
Taking false branch
464 if( strcmp( pFieldStart, "Type" ) == 0 ) { pInfo->parseFieldFunc[ fieldNo ] = parseFieldMsgType; resultFlag = true1; }
11
Assuming the condition is false
12
Taking false branch
465 if( strcmp( pFieldStart, "ID" ) == 0 ) { pInfo->parseFieldFunc[ fieldNo ] = parseFieldID; resultFlag = true1; }
13
Assuming the condition is false
14
Taking false branch
466 if( strcmp( pFieldStart, "Length" ) == 0 ) { pInfo->parseFieldFunc[ fieldNo ] = parseFieldLength; resultFlag = true1; }
15
Assuming the condition is false
16
Taking false branch
467 if( strcmp( pFieldStart, "Data" ) == 0 ) { pInfo->parseFieldFunc[ fieldNo ] = parseFieldData; resultFlag = true1; }
17
Assuming the condition is false
18
Taking false branch
468
469 /* Set start of next field to end of previous + 1 */
470 pFieldStart = pFieldEnd + 1;
19
Addition of a null pointer (from variable 'pFieldEnd') and a nonzero integer value results in undefined behavior
471 }
472
473 return resultFlag;
474}
475
476/***********************************************************************************************************************
477 * stripTimeStamp
478 *
479 * Strips a time stamp string for any delimiters
480 **********************************************************************************************************************/
481static uint8_t stripTimeStamp( const cCLLog_logFileInfo_t *pInfo, char *pTimeStampString )
482{
483 uint8_t strippedLength = 0U;
484
485 /* Char by char, strip the delimiters from the time stamp string */
486 size_t timeStampStringLen = strlen( pTimeStampString );
487 for (size_t i = 0U; i < timeStampStringLen; i++ )
488 {
489 /* Get char */
490 char charTmp = pTimeStampString[i];
491
492 /* If delimiter, skip */
493 if( charTmp == pInfo->separator ){ continue; }
494 if( charTmp == pInfo->timeSeparator ){ continue; }
495 if( charTmp == pInfo->timeSeparatorMs ){ continue; }
496 if( charTmp == pInfo->dateSeparator ){ continue; }
497 if( charTmp == pInfo->dateAndTimeSeparator ){ continue; }
498
499 /* Not a delimiter, keep char */
500 pTimeStampString[ strippedLength++ ] = charTmp;
501 }
502 pTimeStampString[ strippedLength ] = '\0';
503
504 return strippedLength;
505}
506
507static bool_Bool parseString(const char *pFieldValue, char *valuep, size_t valueSize, char *fieldName, int *err, char **err_info)
508{
509 if (!checked_strcpy(valuep, valueSize, pFieldValue))
510 {
511 *err = WTAP_ERR_BAD_FILE-13;
512 *err_info = ws_strdup_printf("cllog: %s is too long",wmem_strdup_printf(((void*)0), "cllog: %s is too long", fieldName
)
513 fieldName)wmem_strdup_printf(((void*)0), "cllog: %s is too long", fieldName
)
;
514 return false0;
515 }
516 return true1;
517}
518
519static bool_Bool parseUnsigned(const char *pFieldValue, uint32_t *valuep, char *fieldName, int *err, char **err_info)
520{
521 uint32_t value;
522
523 if (!wtap_strtou32("cllog", fieldName, pFieldValue, &value, err, err_info)) {
524 return false0;
525 }
526 *valuep = value;
527 return true1;
528}
529
530static bool_Bool parseSeparator(const char *pFieldValue, char *separatorp, char *fieldName, int *err, char **err_info)
531{
532 char separator = '\0';
533
534 /* Separator field is if set e.g. ";" - that is 3 chars. Else it is "" */
535 if (strlen( pFieldValue) == 3)
536 {
537 if (pFieldValue[0] != '"' || !g_ascii_isprint(pFieldValue[1])((g_ascii_table[(guchar) (pFieldValue[1])] & G_ASCII_PRINT
) != 0)
||
538 pFieldValue[2] != '"')
539 {
540 *err = WTAP_ERR_BAD_FILE-13;
541 *err_info = ws_strdup_printf("cllog: %s separator is not valid",wmem_strdup_printf(((void*)0), "cllog: %s separator is not valid"
, fieldName)
542 fieldName)wmem_strdup_printf(((void*)0), "cllog: %s separator is not valid"
, fieldName)
;
543 return false0;
544 }
545 separator = pFieldValue[1];
546 }
547 *separatorp = separator;
548 return true1;
549}
550
551static bool_Bool parseBoolean(const char *pFieldValue, bool_Bool *value, char *fieldName, int *err, char **err_info)
552{
553 if (strcmp(pFieldValue, "true") == 0)
554 {
555 *value = true1;
556 }
557 else if (strcmp(pFieldValue, "false") == 0)
558 {
559 *value = false0;
560 }
561 else
562 {
563 *err = WTAP_ERR_BAD_FILE-13;
564 *err_info = ws_strdup_printf("cllog: %s value is not valid",wmem_strdup_printf(((void*)0), "cllog: %s value is not valid"
, fieldName)
565 fieldName)wmem_strdup_printf(((void*)0), "cllog: %s value is not valid"
, fieldName)
;
566 return false0;
567 }
568 return true1;
569}
570
571static bool_Bool parseLogFileHeaderLine_type(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
572{
573 if (strcmp(pFieldValue, "CANLogger1000") == 0 || strcmp(pFieldValue, "CL1000") == 0)
574 {
575 pInfo->loggerType = type_CL1000_e;
576 }
577 else if (strcmp(pFieldValue, "CANLogger2000") == 0 || strcmp(pFieldValue, "CL2000") == 0)
578 {
579 pInfo->loggerType = type_CL2000_e;
580 }
581 else if (strcmp(pFieldValue, "CANLogger3000") == 0 || strcmp(pFieldValue, "CL3000") == 0)
582 {
583 pInfo->loggerType = type_CL3000_e;
584 }
585 else
586 {
587 *err = WTAP_ERR_BAD_FILE-13;
588 *err_info = g_strdup("cllog: logger type value is not valid")g_strdup_inline ("cllog: logger type value is not valid");
589 return false0;
590 }
591 return true1;
592}
593
594static bool_Bool parseLogFileHeaderLine_hwrev(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
595{
596 return parseString(pFieldValue, pInfo->hwrev, sizeof pInfo->hwrev, "hardware revision", err, err_info);
597}
598
599static bool_Bool parseLogFileHeaderLine_fwrev(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
600{
601 return parseString(pFieldValue, pInfo->fwrev, sizeof pInfo->fwrev, "firmware revision", err, err_info);
602}
603
604static bool_Bool parseLogFileHeaderLine_id(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
605{
606 return parseString(pFieldValue, pInfo->id, sizeof pInfo->id, "ID", err, err_info);
607}
608
609static bool_Bool parseLogFileHeaderLine_sessionNo(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
610{
611 return parseUnsigned(pFieldValue, &pInfo->sessionNo, "session number", err, err_info);
612}
613
614static bool_Bool parseLogFileHeaderLine_splitNo(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
615{
616 return parseUnsigned(pFieldValue, &pInfo->splitNo, "split number", err, err_info);
617}
618
619static bool_Bool parseLogFileHeaderLine_time(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
620{
621 struct tm tm;
622
623 memset(&tm, 0, sizeof tm);
624 /* YYYYMMDDThhmmss */
625 sscanf(pFieldValue,
626 "%4u%2u%2uT%2u%2u%2u",
627 &tm.tm_year,
628 &tm.tm_mon,
629 &tm.tm_mday,
630 &tm.tm_hour,
631 &tm.tm_min,
632 &tm.tm_sec);
633 tm.tm_mon -= 1;
634 tm.tm_year -= 1900;
635
636 /* To Epoch ( mktime converts to epoch from local (!!!) timezone )*/
637 pInfo->logStartTime.secs = mktime(&tm);
638 pInfo->logStartTime.nsecs = 0;
639
640 if (!checked_strcpy(pInfo->logStartTimeString, sizeof pInfo->logStartTimeString, pFieldValue))
641 {
642 *err = WTAP_ERR_BAD_FILE-13;
643 *err_info = g_strdup("cllog: time is too long")g_strdup_inline ("cllog: time is too long");
644 return false0;
645 }
646 return true1;
647}
648
649static bool_Bool parseLogFileHeaderLine_valueSeparator(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
650{
651 return parseSeparator(pFieldValue, &pInfo->separator, "value", err, err_info);
652}
653
654static bool_Bool parseLogFileHeaderLine_timeFormat(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
655{
656 uint32_t format;
657
658 if (!wtap_strtou32("cllog", "time format", pFieldValue, &format, err, err_info))
659 {
660 return false0;
661 }
662 if (format > 6)
663 {
664 *err = WTAP_ERR_BAD_FILE-13;
665 *err_info = g_strdup("cllog: time format value is not valid")g_strdup_inline ("cllog: time format value is not valid");
666 return false0;
667 }
668 pInfo->timeFormat = (uint8_t)format;
669 return true1;
670}
671
672static bool_Bool parseLogFileHeaderLine_timeSeparator(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
673{
674 return parseSeparator(pFieldValue, &pInfo->timeSeparator, "time", err, err_info);
675}
676
677static bool_Bool parseLogFileHeaderLine_timeSeparatorMs(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
678{
679 return parseSeparator(pFieldValue, &pInfo->timeSeparatorMs, "time millisecond", err, err_info);
680}
681
682static bool_Bool parseLogFileHeaderLine_dateSeparator(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
683{
684 return parseSeparator(pFieldValue, &pInfo->dateSeparator, "date", err, err_info);
685}
686
687static bool_Bool parseLogFileHeaderLine_timeAndDateSeparator(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
688{
689 return parseSeparator(pFieldValue, &pInfo->dateAndTimeSeparator, "date and time", err, err_info);
690}
691
692static bool_Bool parseLogFileHeaderLine_bitRate(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
693{
694 return parseUnsigned(pFieldValue, &pInfo->bitRate, "bit rate", err, err_info);
695}
696
697static bool_Bool parseLogFileHeaderLine_silentMode(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
698{
699 bool_Bool silentMode;
700
701 if (!parseBoolean(pFieldValue, &silentMode, "silent mode", err, err_info))
702 {
703 return false0;
704 }
705
706 if (silentMode)
707 {
708 pInfo->silentMode = silent_enabled_e;
709 }
710 else
711 {
712 pInfo->silentMode = silent_disabled_e;
713 }
714 return true1;
715}
716
717static bool_Bool parseLogFileHeaderLine_cyclicMode(cCLLog_logFileInfo_t *pInfo, char *pFieldValue, int *err, char **err_info)
718{
719 bool_Bool cyclicMode;
720
721 if (!parseBoolean(pFieldValue, &cyclicMode, "silent mode", err, err_info))
722 {
723 return false0;
724 }
725
726 if (cyclicMode)
727 {
728 pInfo->cyclicMode = cyclic_enabled_e;
729 }
730 else
731 {
732 pInfo->cyclicMode = cyclic_disabled_e;
733 }
734 return true1;
735}
736
737static bool_Bool
738cllog_read_common(wtap *wth, FILE_T fh, wtap_rec *rec, int *err, char **err_info)
739{
740 cCLLog_logFileInfo_t* clLog = (cCLLog_logFileInfo_t*)wtap_socketcan_get_private_data(wth);
741 char line[MAX_LOG_LINE_LENGTH200];
742 cCLLog_message_t logEntry;
743
744 /* Read a line */
745 if (!wtap_read_line_or_eof(fh, line, sizeof(line), NULL((void*)0), err, err_info))
746 {
747 /* EOF or error. */
748 return false0;
749 }
750
751 /* Default the log entry structure */
752 memset(&logEntry, 0, sizeof(logEntry));
753
754 /* Parse the line */
755 if (!parseLogLine(clLog, line, &logEntry, err, err_info))
756 {
757 return false0;
758 }
759
760 logEntry.msg.interface_id = wtap_socketcan_find_or_create_new_interface(wth, clLog->id);
761 if (!wtap_socketcan_gen_packet(wth, rec, &logEntry.msg, "cllog", err, err_info))
762 return false0;
763
764
765 if (logEntry.msgType == msg_tx_standard_e || logEntry.msgType == msg_tx_extended_e)
766 {
767 wtap_block_add_uint32_option(rec->block, OPT_PKT_FLAGS2, PACK_FLAGS_DIRECTION_OUTBOUND2);
768 }
769 else if (logEntry.msgType == msg_rx_standard_e || logEntry.msgType == msg_rx_extended_e)
770 {
771 wtap_block_add_uint32_option(rec->block, OPT_PKT_FLAGS2, PACK_FLAGS_DIRECTION_INBOUND1);
772 }
773
774 return true1;
775}
776
777static bool_Bool
778cllog_read(wtap *wth, wtap_rec *rec, int *err, char **err_info, int64_t *data_offset)
779{
780 *data_offset = file_tell(wth->fh);
781
782 return cllog_read_common(wth, wth->fh, rec, err, err_info);
783}
784
785static bool_Bool
786cllog_seek_read(wtap *wth, int64_t seek_off, wtap_rec *rec, int *err, char **err_info)
787{
788 if (file_seek(wth->random_fh, seek_off, SEEK_SET0, err) == -1)
789 return false0;
790
791 return cllog_read_common(wth, wth->random_fh, rec, err, err_info);
792}
793
794static void
795cllog_close(void* tap_data)
796{
797 /* Clean up the file information */
798 g_free((cCLLog_logFileInfo_t*)tap_data)(__builtin_object_size (((cCLLog_logFileInfo_t*)tap_data), 0)
!= ((size_t) - 1)) ? g_free_sized ((cCLLog_logFileInfo_t*)tap_data
, __builtin_object_size (((cCLLog_logFileInfo_t*)tap_data), 0
)) : (g_free) ((cCLLog_logFileInfo_t*)tap_data)
;
799}
800
801wtap_open_return_val
802cllog_open(wtap *wth, int *err, char **err_info)
803{
804 cCLLog_logFileInfo_t *clLog;
805 char line[ MAX_LOG_LINE_LENGTH200 ];
806 bool_Bool status;
807 char *linep;
808 bool_Bool saw_header_line = false0;
809
810 clLog = g_new0(cCLLog_logFileInfo_t, 1)((cCLLog_logFileInfo_t *) g_malloc0_n ((1), sizeof (cCLLog_logFileInfo_t
)))
;
811
812 /*
813 * We're at the beginning of the file. The header is a set
814 * of comment lines, each beginning with a '#'. Read each line,
815 * stopping if we see a non-comment line, and parse each
816 * comment line; if any aren't valid, quit and indicate that
817 * this isn't a CLX log file.
818 */
819 while ((status = wtap_read_line_or_eof(wth->fh, line, sizeof(line), NULL((void*)0), err, err_info)) &&
820 line[0] == '#')
821 {
822 linep = line;
823
824 /*
825 * Skip the comment character and white space following it.
826 */
827 linep++;
828 while (*linep == ' ' || *linep == '\t')
829 linep++;
830
831 if (*linep == '\0')
832 {
833 /*
834 * Skip over empty comment lines.
835 * XXX - should we treat that as an indication of an
836 * invalid file?
837 */
838 continue;
839 }
840
841 /*
842 * Look for the handler for this particular header line.
843 */
844 for (unsigned int i = 0U; i < HEADER_LINE_PARSE_MAPPING_LENGTH(sizeof (headerLineParseMapping) / sizeof (headerLineParseMapping
)[0])
; i++)
845 {
846 const headerLineParseMapping_t *pHeaderMapping = &headerLineParseMapping[i];
847 size_t matchStringLen = strlen(pHeaderMapping->pMatchString);
848
849 if (strncmp(linep, pHeaderMapping->pMatchString, matchStringLen) == 0 &&
850 pHeaderMapping->parseFunc != NULL((void*)0))
851 {
852 /*
853 * This matches this header value.
854 * Skip past the tag.
855 */
856 linep += matchStringLen;
857
858 /* Replace any newline chars with end of line */
859 for (char *pChar = linep; ; pChar++)
860 {
861 if (*pChar == '\n' || *pChar == '\r' || *pChar == '\0')
862 {
863 *pChar = '\0';
864 break;
865 }
866 }
867
868 /*
869 * Call the handler.
870 */
871 if (!pHeaderMapping->parseFunc(clLog, linep, err, err_info))
872 {
873 /*
874 * XXX - should this file be rejected as not
875 * one of ours? Given the line looks like
876 * a comment that begins with a valid header
877 * field tag, it may be likely to be one of
878 * ours.
879 */
880 g_free(clLog)(__builtin_object_size ((clLog), 0) != ((size_t) - 1)) ? g_free_sized
(clLog, __builtin_object_size ((clLog), 0)) : (g_free) (clLog
)
;
881 if (*err == WTAP_ERR_BAD_FILE-13)
882 {
883 wmem_free(NULL((void*)0), *err_info);
884 *err_info = NULL((void*)0);
885 }
886 return WTAP_OPEN_NOT_MINE;
887 }
888 saw_header_line = true1;
889 }
890 }
891 }
892
893 /*
894 * Did wtap_read_line_or_eof() fail?
895 */
896 if (!status)
897 {
898 /*
899 * Yes - wtap_read_line_or_eof() didn't return a line.
900 * Did it get an error?
901 */
902 if (*err != 0)
903 {
904 /* Yes. What was it? */
905 if (*err == WTAP_ERR_SHORT_READ-12)
906 {
907 /* Incomplete header, so not ours. */
908 g_free(clLog)(__builtin_object_size ((clLog), 0) != ((size_t) - 1)) ? g_free_sized
(clLog, __builtin_object_size ((clLog), 0)) : (g_free) (clLog
)
;
909 return WTAP_OPEN_NOT_MINE;
910 }
911 else
912 {
913 /* I/O error. */
914 g_free(clLog)(__builtin_object_size ((clLog), 0) != ((size_t) - 1)) ? g_free_sized
(clLog, __builtin_object_size ((clLog), 0)) : (g_free) (clLog
)
;
915 return WTAP_OPEN_ERROR;
916 }
917 }
918
919 /*
920 * No, it just got an EOF. If we saw at least one valid
921 * header line, treat it as our file, as older versions did
922 * so; it might be the log of a session that logged nothing.
923 *
924 * Otherwise, it's empty or consists only of other comment
925 * lines, which could be any of a number of file types
926 * (e.g., configuration files), so it's not ours.
927 *
928 * XXX - should we treat it as not our file in any case, as it
929 * lacks the column header line?
930 */
931 if (!saw_header_line)
932 {
933 g_free(clLog)(__builtin_object_size ((clLog), 0) != ((size_t) - 1)) ? g_free_sized
(clLog, __builtin_object_size ((clLog), 0)) : (g_free) (clLog
)
;
934 return WTAP_OPEN_NOT_MINE;
935 }
936 }
937 else
938 {
939 /*
940 * We've read the first line after the header, so it's the column
941 * header line. Parse it. This requires that we have been provided with a
942 * valid separator.
943 */
944 if (clLog->separator == '\0' || !parseColumnHeaderFields(clLog, line))
945 {
946 g_free(clLog)(__builtin_object_size ((clLog), 0) != ((size_t) - 1)) ? g_free_sized
(clLog, __builtin_object_size ((clLog), 0)) : (g_free) (clLog
)
;
947 return WTAP_OPEN_NOT_MINE;
948 }
949 }
950
951 wtap_set_as_socketcan(wth, cllog_file_type_subtype, WTAP_TSPREC_MSEC3, clLog, cllog_close);
952 wth->snapshot_length = 0;
953
954 wth->subtype_read = cllog_read;
955 wth->subtype_seek_read = cllog_seek_read;
956
957 return WTAP_OPEN_MINE;
958}
959
960/* Options for packet blocks. */
961static const struct supported_option_type packet_block_options_supported[] = {
962 { OPT_PKT_FLAGS2, ONE_OPTION_SUPPORTED },
963};
964
965static const struct supported_block_type cllog_blocks_supported[] = {
966 /*
967 * We support packet blocks, with only the flags option supported.
968 */
969 { WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(packet_block_options_supported)(sizeof (packet_block_options_supported) / sizeof (packet_block_options_supported
)[0]), packet_block_options_supported
}
970};
971
972static const struct file_type_subtype_info cllog_info = {
973 "CSS Electronics CLX000 CAN log", "cllog", "txt", NULL((void*)0),
974 false0, BLOCKS_SUPPORTED(cllog_blocks_supported)(sizeof (cllog_blocks_supported) / sizeof (cllog_blocks_supported
)[0]), cllog_blocks_supported
,
975 NULL((void*)0), NULL((void*)0), NULL((void*)0)
976};
977
978void
979register_canlogger(void)
980{
981 cllog_file_type_subtype = wtap_register_file_type_subtype(&cllog_info);
982}