@@ -195,25 +195,34 @@ static ssize_t read_log_tail(const char *log_file_path, char *buffer,
195
195
static char * find_dircache_stats_line (const char * buffer , size_t bytes_read )
196
196
{
197
197
char * stats_line = NULL ;
198
+ /* Make a working copy of the buffer to avoid const issues */
199
+ char * work_buffer = malloc (bytes_read + 1 );
200
+
201
+ if (!work_buffer ) {
202
+ return NULL ;
203
+ }
204
+
205
+ memcpy (work_buffer , buffer , bytes_read );
206
+ work_buffer [bytes_read ] = '\0' ;
198
207
/* Search backwards through buffer for the LAST dircache statistics line */
199
- char * current = ( char * ) buffer + bytes_read ;
208
+ char * current = work_buffer + bytes_read ;
200
209
201
- while (current > buffer ) {
210
+ while (current > work_buffer ) {
202
211
/* Find end of previous line */
203
212
char * line_end = current - 1 ;
204
213
205
- while (line_end >= buffer && * line_end == '\n' ) {
214
+ while (line_end >= work_buffer && * line_end == '\n' ) {
206
215
line_end -- ;
207
216
}
208
217
209
- if (line_end < buffer ) {
218
+ if (line_end < work_buffer ) {
210
219
break ;
211
220
}
212
221
213
222
/* Find start of this line */
214
223
char * line_start = line_end ;
215
224
216
- while (line_start > buffer && * (line_start - 1 ) != '\n' ) {
225
+ while (line_start > work_buffer && * (line_start - 1 ) != '\n' ) {
217
226
line_start -- ;
218
227
}
219
228
@@ -234,6 +243,7 @@ static char *find_dircache_stats_line(const char *buffer, size_t bytes_read)
234
243
current = line_start ;
235
244
}
236
245
246
+ free (work_buffer );
237
247
return stats_line ;
238
248
}
239
249
@@ -245,27 +255,37 @@ static void display_last_log_lines(const char *buffer, size_t bytes_read)
245
255
printf ("(At least 'log level = default:info' is required)\n" );
246
256
printf ("Last 10 lines of log file:\n" );
247
257
printf ("---------------------------\n" );
258
+ /* Make a working copy of the buffer to avoid const issues */
259
+ char * work_buffer = malloc (bytes_read + 1 );
260
+
261
+ if (!work_buffer ) {
262
+ printf ("Memory allocation failed\n" );
263
+ return ;
264
+ }
265
+
266
+ memcpy (work_buffer , buffer , bytes_read );
267
+ work_buffer [bytes_read ] = '\0' ;
248
268
/* Search backwards through buffer for last 10 lines */
249
269
char * last_lines [MAX_LOG_LINES_DISPLAY ] = {NULL };
250
270
int line_count = 0 ;
251
- char * curr = ( char * ) buffer + bytes_read ;
271
+ char * curr = work_buffer + bytes_read ;
252
272
253
- while (curr > buffer && line_count < MAX_LOG_LINES_DISPLAY ) {
273
+ while (curr > work_buffer && line_count < MAX_LOG_LINES_DISPLAY ) {
254
274
/* Find end of previous line */
255
275
char * l_end = curr - 1 ;
256
276
257
- while (l_end >= buffer && * l_end == '\n' ) {
277
+ while (l_end >= work_buffer && * l_end == '\n' ) {
258
278
l_end -- ;
259
279
}
260
280
261
- if (l_end < buffer ) {
281
+ if (l_end < work_buffer ) {
262
282
break ;
263
283
}
264
284
265
285
/* Find start of this line */
266
286
char * l_start = l_end ;
267
287
268
- while (l_start > buffer && * (l_start - 1 ) != '\n' ) {
288
+ while (l_start > work_buffer && * (l_start - 1 ) != '\n' ) {
269
289
l_start -- ;
270
290
}
271
291
@@ -285,12 +305,13 @@ static void display_last_log_lines(const char *buffer, size_t bytes_read)
285
305
curr = l_start ;
286
306
}
287
307
308
+ free (work_buffer );
309
+
288
310
/* Print the last lines in correct order (reverse read order) */
289
311
for (int i = line_count - 1 ; i >= 0 ; i -- ) {
290
312
if (last_lines [i ]) {
291
313
/* Get length while pointer is known to be non-NULL */
292
- size_t len = strlen (
293
- last_lines [i ]); /* NOSONAR: Pointer verified non-NULL in enclosing if statement */
314
+ size_t len = strlen (last_lines [i ]); // NOSONAR: Pointer verified non-NULL
294
315
printf ("%s" , last_lines [i ]);
295
316
296
317
/* Add newline if not present */
@@ -335,8 +356,7 @@ static void display_dircache_statistics(void)
335
356
336
357
if (stats_line ) {
337
358
/* Get length while pointer is known to be non-NULL */
338
- size_t len = strlen (
339
- stats_line ); /* NOSONAR: Pointer verified non-NULL in enclosing if statement */
359
+ size_t len = strlen (stats_line ); // NOSONAR: Pointer verified non-NULL
340
360
printf ("%s" , stats_line );
341
361
342
362
if (len > 0 && stats_line [len - 1 ] != '\n' ) {
0 commit comments