[sysprof: 18/63] libsysprof-capture: Use strlcpy() instead of g_strlcpy()
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [sysprof: 18/63] libsysprof-capture: Use strlcpy() instead of g_strlcpy()
- Date: Sat, 4 Jul 2020 18:30:51 +0000 (UTC)
commit 6e281dca1f113ad6746a6f205633d38ff6c84ac6
Author: Philip Withnall <withnall endlessm com>
Date: Wed Jul 1 17:08:16 2020 +0100
libsysprof-capture: Use strlcpy() instead of g_strlcpy()
If the system doesn’t provide `strlcpy()` (FreeBSD does, Linux doesn’t),
use an inbuilt copy instead.
Signed-off-by: Philip Withnall <withnall endlessm com>
Helps: #40
meson.build | 2 ++
.../sysprof-capture-util-private.h | 7 +++++++
src/libsysprof-capture/sysprof-capture-util.c | 20 ++++++++++++++++++++
src/libsysprof-capture/sysprof-capture-writer.c | 16 ++++++++--------
src/libsysprof-capture/sysprof-collector.c | 8 ++++----
5 files changed, 41 insertions(+), 12 deletions(-)
---
diff --git a/meson.build b/meson.build
index 90dc5fb..6550126 100644
--- a/meson.build
+++ b/meson.build
@@ -91,6 +91,8 @@ if cc.has_header('execinfo.h')
config_h.set10('HAVE_EXECINFO_H', true)
endif
+config_h.set('HAVE_STRLCPY', cc.has_function('strlcpy'))
+
if get_option('libunwind')
libunwind_dep = dependency('libunwind-generic', required: false)
if libunwind_dep.found()
diff --git a/src/libsysprof-capture/sysprof-capture-util-private.h
b/src/libsysprof-capture/sysprof-capture-util-private.h
index 26ba79e..44c7d0d 100644
--- a/src/libsysprof-capture/sysprof-capture-util-private.h
+++ b/src/libsysprof-capture/sysprof-capture-util-private.h
@@ -89,5 +89,12 @@ ssize_t _sysprof_sendfile (int out_fd,
int in_fd,
off_t *offset,
size_t count);
+#endif
+#ifdef HAVE_STRLCPY
+# define _sysprof_strlcpy(d,s,ds) strlcpy(d,s,ds)
+#else
+size_t _sysprof_strlcpy (char *dest,
+ const char *src,
+ size_t dest_size);
#endif
diff --git a/src/libsysprof-capture/sysprof-capture-util.c b/src/libsysprof-capture/sysprof-capture-util.c
index 75f9b26..d2e9835 100644
--- a/src/libsysprof-capture/sysprof-capture-util.c
+++ b/src/libsysprof-capture/sysprof-capture-util.c
@@ -236,3 +236,23 @@ ssize_t
errno = 0;
return total;
}
+
+size_t
+(_sysprof_strlcpy) (char *dest,
+ const char *src,
+ size_t dest_size)
+{
+ size_t i = 0;
+
+ if (dest_size > 0)
+ {
+ for (; i < dest_size - 1 && src[i] != '\0'; i++)
+ dest[i] = src[i];
+ dest[i] = '\0';
+ }
+
+ for (; src[i] != '\0'; i++)
+ ;
+
+ return i;
+}
diff --git a/src/libsysprof-capture/sysprof-capture-writer.c b/src/libsysprof-capture/sysprof-capture-writer.c
index 5036eda..23f0024 100644
--- a/src/libsysprof-capture/sysprof-capture-writer.c
+++ b/src/libsysprof-capture/sysprof-capture-writer.c
@@ -519,7 +519,7 @@ sysprof_capture_writer_new_from_fd (int fd,
header->little_endian = false;
#endif
header->padding = 0;
- g_strlcpy (header->capture_time, nowstr, sizeof header->capture_time);
+ _sysprof_strlcpy (header->capture_time, now_str, sizeof header->capture_time);
header->time = SYSPROF_CAPTURE_CURRENT_TIME;
header->end_time = 0;
memset (header->suffix, 0, sizeof header->suffix);
@@ -599,7 +599,7 @@ sysprof_capture_writer_add_map (SysprofCaptureWriter *self,
ev->offset = offset;
ev->inode = inode;
- g_strlcpy (ev->filename, filename, len - sizeof *ev);
+ _sysprof_strlcpy (ev->filename, filename, len - sizeof *ev);
ev->filename[len - sizeof *ev - 1] = '\0';
self->stat.frame_count[SYSPROF_CAPTURE_FRAME_MAP]++;
@@ -642,8 +642,8 @@ sysprof_capture_writer_add_mark (SysprofCaptureWriter *self,
SYSPROF_CAPTURE_FRAME_MARK);
ev->duration = duration;
- g_strlcpy (ev->group, group, sizeof ev->group);
- g_strlcpy (ev->name, name, sizeof ev->name);
+ _sysprof_strlcpy (ev->group, group, sizeof ev->group);
+ _sysprof_strlcpy (ev->name, name, sizeof ev->name);
memcpy (ev->message, message, message_len);
self->stat.frame_count[SYSPROF_CAPTURE_FRAME_MARK]++;
@@ -687,7 +687,7 @@ sysprof_capture_writer_add_metadata (SysprofCaptureWriter *self,
time,
SYSPROF_CAPTURE_FRAME_METADATA);
- g_strlcpy (ev->id, id, sizeof ev->id);
+ _sysprof_strlcpy (ev->id, id, sizeof ev->id);
memcpy (ev->metadata, metadata, metadata_len);
ev->metadata[metadata_len] = 0;
@@ -743,7 +743,7 @@ sysprof_capture_writer_add_process (SysprofCaptureWriter *self,
time,
SYSPROF_CAPTURE_FRAME_PROCESS);
- g_strlcpy (ev->cmdline, cmdline, len - sizeof *ev);
+ _sysprof_strlcpy (ev->cmdline, cmdline, len - sizeof *ev);
ev->cmdline[len - sizeof *ev - 1] = '\0';
self->stat.frame_count[SYSPROF_CAPTURE_FRAME_PROCESS]++;
@@ -1409,7 +1409,7 @@ sysprof_capture_writer_add_log (SysprofCaptureWriter *self,
ev->severity = severity & 0xFFFF;
ev->padding1 = 0;
ev->padding2 = 0;
- g_strlcpy (ev->domain, domain, sizeof ev->domain);
+ _sysprof_strlcpy (ev->domain, domain, sizeof ev->domain);
memcpy (ev->message, message, message_len);
self->stat.frame_count[SYSPROF_CAPTURE_FRAME_LOG]++;
@@ -1447,7 +1447,7 @@ sysprof_capture_writer_add_file (SysprofCaptureWriter *self,
ev->padding1 = 0;
ev->is_last = !!is_last;
ev->len = data_len;
- g_strlcpy (ev->path, path, sizeof ev->path);
+ _sysprof_strlcpy (ev->path, path, sizeof ev->path);
memcpy (ev->data, data, data_len);
self->stat.frame_count[SYSPROF_CAPTURE_FRAME_FILE_CHUNK]++;
diff --git a/src/libsysprof-capture/sysprof-collector.c b/src/libsysprof-capture/sysprof-collector.c
index 9c72d0b..db17756 100644
--- a/src/libsysprof-capture/sysprof-collector.c
+++ b/src/libsysprof-capture/sysprof-collector.c
@@ -406,8 +406,8 @@ sysprof_collector_mark (int64_t time,
ev->frame.pid = collector->pid;
ev->frame.time = time;
ev->duration = duration;
- g_strlcpy (ev->group, group, sizeof ev->group);
- g_strlcpy (ev->name, mark, sizeof ev->name);
+ _sysprof_strlcpy (ev->group, group, sizeof ev->group);
+ _sysprof_strlcpy (ev->name, mark, sizeof ev->name);
memcpy (ev->message, message, sl);
ev->message[sl] = 0;
@@ -446,7 +446,7 @@ sysprof_collector_log (int severity,
ev->severity = severity & 0xFFFF;
ev->padding1 = 0;
ev->padding2 = 0;
- g_strlcpy (ev->domain, domain, sizeof ev->domain);
+ _sysprof_strlcpy (ev->domain, domain, sizeof ev->domain);
memcpy (ev->message, message, sl);
ev->message[sl] = 0;
@@ -489,7 +489,7 @@ sysprof_collector_log_printf (int severity,
ev->severity = severity & 0xFFFF;
ev->padding1 = 0;
ev->padding2 = 0;
- g_strlcpy (ev->domain, domain, sizeof ev->domain);
+ _sysprof_strlcpy (ev->domain, domain, sizeof ev->domain);
memcpy (ev->message, formatted, sl);
ev->message[sl] = 0;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]