From 0c6fcea79ba0aefe2e70421a66e699631d8c75d1 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Mon, 5 Jun 2023 15:27:16 -0700 Subject: [PATCH] libsysprof-profile: avoid splitting string into lines We will always have either \n or \0 at the end, so we can just sscanf this without needing to split strings. The sscanf() is still our top offender though, and so we should probably look for other ways to parse this string than sscanf(). --- src/libsysprof-profile/sysprof-linux-instrument.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/libsysprof-profile/sysprof-linux-instrument.c b/src/libsysprof-profile/sysprof-linux-instrument.c index b9dd09e1..cc4a55fb 100644 --- a/src/libsysprof-profile/sysprof-linux-instrument.c +++ b/src/libsysprof-profile/sysprof-linux-instrument.c @@ -26,6 +26,8 @@ #include "sysprof-podman-private.h" #include "sysprof-recording-private.h" +#include "line-reader-private.h" + struct _SysprofLinuxInstrument { SysprofInstrument parent_instance; @@ -53,26 +55,28 @@ add_mmaps (SysprofRecording *recording, gboolean ignore_inode) { SysprofCaptureWriter *writer; - g_auto(GStrv) lines = NULL; + LineReader reader; + const char *line; + gsize line_len; g_assert (SYSPROF_IS_RECORDING (recording)); g_assert (mapsstr != NULL); g_assert (pid > 0); writer = _sysprof_recording_writer (recording); - lines = g_strsplit (mapsstr, "\n", 0); - for (guint i = 0; lines[i] != NULL; i++) + line_reader_init (&reader, (char *)mapsstr, -1); + while ((line = line_reader_next (&reader, &line_len))) { char file[512]; gulong start; gulong end; gulong offset; gulong inode; - int r; gboolean is_vdso; + int r; - r = sscanf (lines[i], + r = sscanf (line, "%lx-%lx %*15s %lx %*x:%*x %lu %511[^\n]", &start, &end, &offset, &inode, file); file [sizeof file - 1] = '\0';