From 1182b65c078091c5766cda93e5ec4307dc5e78d6 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Thu, 27 Apr 2023 16:59:41 -0700 Subject: [PATCH] libsysprof-analyze: add CString helper This checks, up to the end of the frame, that we have a valid \0 for the CString before passing it back to the caller. Otherwise, NULL is returned for a corrupt/invalid CString within the frame. --- .../sysprof-document-frame-private.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/libsysprof-analyze/sysprof-document-frame-private.h b/src/libsysprof-analyze/sysprof-document-frame-private.h index 918acb9d..96949ac4 100644 --- a/src/libsysprof-analyze/sysprof-document-frame-private.h +++ b/src/libsysprof-analyze/sysprof-document-frame-private.h @@ -80,4 +80,19 @@ SysprofDocumentFrame *_sysprof_document_frame_new (GMappedFile *ma (SYSPROF_DOCUMENT_FRAME_NEEDS_SWAP(obj) ? GINT64_TO_BE(val) : (val)) #endif +static inline const char * +SYSPROF_DOCUMENT_FRAME_CSTRING (SysprofDocumentFrame *self, + const char *str) +{ + const char *endptr = (const char *)self->frame + self->frame_len; + + for (const char *c = str; c < endptr; c++) + { + if (*c == 0) + return str; + } + + return NULL; +} + G_END_DECLS