public inbox for dtrace@lists.linux.dev
 help / color / mirror / Atom feed
* [PATCH v2 2/2] Use asprintf() to allocate strings
@ 2026-01-16 22:57 eugene.loh
  2026-01-27 15:36 ` Kris Van Hees
  0 siblings, 1 reply; 2+ messages in thread
From: eugene.loh @ 2026-01-16 22:57 UTC (permalink / raw)
  To: dtrace, dtrace-devel

From: Eugene Loh <eugene.loh@oracle.com>

Earlier patches have slowly replaced constructs like:
    int len;
    len = snprintf(NULL, 0, format, args) + 1;
    buf = malloc(len);
    snprintf(buf, len, format, args);
with the more compact:
    asprintf(&buf, format, args);

Replace the remaining instances of the bulkier construct.

Note that dt_conf_init() continues to compute a buffer length and
allocate a buffer, since that buffer will be reused multiple times.

Further, dt_probe_tag() keeps its current form so that memory can be
allocated with alloca() to guard against memory leaks in the event that
ctf_add_typedef() fails.

Signed-off-by: Eugene Loh <eugene.loh@oracle.com>
---
 libdtrace/dt_link.c        | 17 +++++++----------
 libdtrace/dt_prov_dtrace.c | 12 +++---------
 2 files changed, 10 insertions(+), 19 deletions(-)

diff --git a/libdtrace/dt_link.c b/libdtrace/dt_link.c
index c9e0ea5fe..ffa16d9a4 100644
--- a/libdtrace/dt_link.c
+++ b/libdtrace/dt_link.c
@@ -1,6 +1,6 @@
 /*
  * Oracle Linux DTrace.
- * Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2026, Oracle and/or its affiliates. All rights reserved.
  * Licensed under the Universal Permissive License v 1.0 as shown at
  * http://oss.oracle.com/licenses/upl.
  */
@@ -416,7 +416,6 @@ dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
 	char drti[PATH_MAX], symvers[PATH_MAX];
 	int fd, i, cur;
 	char *cmd;
-	size_t len;
 	int ret = 0, status = 0;
 
 	/*
@@ -426,6 +425,7 @@ dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
 	 */
 	if (pgp == NULL) {
 		const char *fmt = "%s -o %s -r";
+		size_t len;
 
 		len = snprintf(NULL, 0, fmt, dtp->dt_ld_path, file) + 1;
 
@@ -521,15 +521,12 @@ dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
 		}
 		snprintf(symvers, sizeof (symvers), "%s/drti/drti-vers", libdir->dir_path);
 
-		len = snprintf(NULL, 0, fmt, dtp->dt_ld_path, emu, file,
-			       symvers, fd, drti) + 1;
+		asprintf(&cmd, fmt, dtp->dt_ld_path, emu, file, symvers, fd,
+			 drti);
+		status = system(cmd);
+		free(cmd);
 
-		cmd = alloca(len);
-
-		(void) snprintf(cmd, len, fmt, dtp->dt_ld_path, emu, file,
-				symvers, fd, drti);
-
-		if ((status = system(cmd)) == -1) {
+		if (status == -1) {
 			ret = dt_link_error(dtp, NULL, -1,
 			    "failed to run %s: %s", dtp->dt_ld_path,
 			    strerror(errno));
diff --git a/libdtrace/dt_prov_dtrace.c b/libdtrace/dt_prov_dtrace.c
index 1bd405b81..9ef001a33 100644
--- a/libdtrace/dt_prov_dtrace.c
+++ b/libdtrace/dt_prov_dtrace.c
@@ -228,7 +228,6 @@ static int attach(dtrace_hdl_t *dtp, const dt_probe_t *prp, int bpf_fd)
 		char	*spec;
 		char	*fn;
 		FILE	*f;
-		size_t	len;
 		int	fd, rc = -1;
 
 		/* get a uprobe specification for this probe */
@@ -248,16 +247,11 @@ static int attach(dtrace_hdl_t *dtp, const dt_probe_t *prp, int bpf_fd)
 			return -ENOENT;
 
 		/* open format file */
-		len = snprintf(NULL, 0, "%s" PROBE_FMT "/format",
-			       EVENTSFS, PROBE_DATA) + 1;
-		fn = dt_alloc(dtp, len);
-		if (fn == NULL)
+		if (asprintf(&fn, "%s" PROBE_FMT "/format", EVENTSFS,
+			     PROBE_DATA) < 0)
 			return -ENOENT;
-
-		snprintf(fn, len, "%s" PROBE_FMT "/format",
-			 EVENTSFS, PROBE_DATA);
 		f = fopen(fn, "r");
-		dt_free(dtp, fn);
+		free(fn);
 		if (f == NULL)
 			return -ENOENT;
 
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2 2/2] Use asprintf() to allocate strings
  2026-01-16 22:57 [PATCH v2 2/2] Use asprintf() to allocate strings eugene.loh
@ 2026-01-27 15:36 ` Kris Van Hees
  0 siblings, 0 replies; 2+ messages in thread
From: Kris Van Hees @ 2026-01-27 15:36 UTC (permalink / raw)
  To: eugene.loh; +Cc: dtrace, dtrace-devel

On Fri, Jan 16, 2026 at 05:57:34PM -0500, eugene.loh@oracle.com wrote:
> From: Eugene Loh <eugene.loh@oracle.com>
> 
> Earlier patches have slowly replaced constructs like:
>     int len;
>     len = snprintf(NULL, 0, format, args) + 1;
>     buf = malloc(len);
>     snprintf(buf, len, format, args);
> with the more compact:
>     asprintf(&buf, format, args);
> 
> Replace the remaining instances of the bulkier construct.
> 
> Note that dt_conf_init() continues to compute a buffer length and
> allocate a buffer, since that buffer will be reused multiple times.
> 
> Further, dt_probe_tag() keeps its current form so that memory can be
> allocated with alloca() to guard against memory leaks in the event that
> ctf_add_typedef() fails.
> 
> Signed-off-by: Eugene Loh <eugene.loh@oracle.com>

Reviewed-by: Kris Van Hees <kris.van.hees@oracle.com>

> ---
>  libdtrace/dt_link.c        | 17 +++++++----------
>  libdtrace/dt_prov_dtrace.c | 12 +++---------
>  2 files changed, 10 insertions(+), 19 deletions(-)
> 
> diff --git a/libdtrace/dt_link.c b/libdtrace/dt_link.c
> index c9e0ea5fe..ffa16d9a4 100644
> --- a/libdtrace/dt_link.c
> +++ b/libdtrace/dt_link.c
> @@ -1,6 +1,6 @@
>  /*
>   * Oracle Linux DTrace.
> - * Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved.
> + * Copyright (c) 2008, 2026, Oracle and/or its affiliates. All rights reserved.
>   * Licensed under the Universal Permissive License v 1.0 as shown at
>   * http://oss.oracle.com/licenses/upl.
>   */
> @@ -416,7 +416,6 @@ dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
>  	char drti[PATH_MAX], symvers[PATH_MAX];
>  	int fd, i, cur;
>  	char *cmd;
> -	size_t len;
>  	int ret = 0, status = 0;
>  
>  	/*
> @@ -426,6 +425,7 @@ dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
>  	 */
>  	if (pgp == NULL) {
>  		const char *fmt = "%s -o %s -r";
> +		size_t len;
>  
>  		len = snprintf(NULL, 0, fmt, dtp->dt_ld_path, file) + 1;
>  
> @@ -521,15 +521,12 @@ dtrace_program_link(dtrace_hdl_t *dtp, dtrace_prog_t *pgp, uint_t dflags,
>  		}
>  		snprintf(symvers, sizeof (symvers), "%s/drti/drti-vers", libdir->dir_path);
>  
> -		len = snprintf(NULL, 0, fmt, dtp->dt_ld_path, emu, file,
> -			       symvers, fd, drti) + 1;
> +		asprintf(&cmd, fmt, dtp->dt_ld_path, emu, file, symvers, fd,
> +			 drti);
> +		status = system(cmd);
> +		free(cmd);
>  
> -		cmd = alloca(len);
> -
> -		(void) snprintf(cmd, len, fmt, dtp->dt_ld_path, emu, file,
> -				symvers, fd, drti);
> -
> -		if ((status = system(cmd)) == -1) {
> +		if (status == -1) {
>  			ret = dt_link_error(dtp, NULL, -1,
>  			    "failed to run %s: %s", dtp->dt_ld_path,
>  			    strerror(errno));
> diff --git a/libdtrace/dt_prov_dtrace.c b/libdtrace/dt_prov_dtrace.c
> index 1bd405b81..9ef001a33 100644
> --- a/libdtrace/dt_prov_dtrace.c
> +++ b/libdtrace/dt_prov_dtrace.c
> @@ -228,7 +228,6 @@ static int attach(dtrace_hdl_t *dtp, const dt_probe_t *prp, int bpf_fd)
>  		char	*spec;
>  		char	*fn;
>  		FILE	*f;
> -		size_t	len;
>  		int	fd, rc = -1;
>  
>  		/* get a uprobe specification for this probe */
> @@ -248,16 +247,11 @@ static int attach(dtrace_hdl_t *dtp, const dt_probe_t *prp, int bpf_fd)
>  			return -ENOENT;
>  
>  		/* open format file */
> -		len = snprintf(NULL, 0, "%s" PROBE_FMT "/format",
> -			       EVENTSFS, PROBE_DATA) + 1;
> -		fn = dt_alloc(dtp, len);
> -		if (fn == NULL)
> +		if (asprintf(&fn, "%s" PROBE_FMT "/format", EVENTSFS,
> +			     PROBE_DATA) < 0)
>  			return -ENOENT;
> -
> -		snprintf(fn, len, "%s" PROBE_FMT "/format",
> -			 EVENTSFS, PROBE_DATA);
>  		f = fopen(fn, "r");
> -		dt_free(dtp, fn);
> +		free(fn);
>  		if (f == NULL)
>  			return -ENOENT;
>  
> -- 
> 2.47.3
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-01-27 15:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-16 22:57 [PATCH v2 2/2] Use asprintf() to allocate strings eugene.loh
2026-01-27 15:36 ` Kris Van Hees

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox