Use asprintf() to allocate strings
PATCH 2/2 Use asprintf() to allocate strings
From: Eugene Loh <[email protected]>
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.
--- libdtrace/dt_link.c | 17 +++++++---------- libdtrace/dt_probe.c | 11 +++-------- libdtrace/dt_prov_dtrace.c | 12 +++--------- 3 files changed, 13 insertions(+), 27 deletions(-)
libdtrace/dt_link.c +7 -10
@@ -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));
libdtrace/dt_probe.c +3 -8
@@ -1,6 +1,6 @@/* * Oracle Linux DTrace. -* Copyright (c) 2006, 2025, Oracle and/or its affiliates. All rights reserved. +* Copyright (c) 2006, 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. */
@@ -475,15 +475,9 @@ dt_probe_tag(dt_probe_t *prp, uint_t argn, dt_node_t *dnp) {dtrace_hdl_t *dtp = prp->prov->pv_hdl;dtrace_typeinfo_t dtt; -size_t len;char *tag; - len =snprintf (NULL ,0 ,"__dtrace_%s___%s_arg%u" , - prp->prov->desc.dtvd_name, prp->pr_name, argn); - - tag =alloca (len +1 ); - -snprintf (tag, len +1 ,"__dtrace_%s___%s_arg%u" , +asprintf (&tag,"__dtrace_%s___%s_arg%u" , prp->prov->desc.dtvd_name, prp->pr_name, argn);if (dtrace_lookup_by_type (dtp,DTRACE_OBJ_DDEFS , tag, &dtt) !=0 ) {
@@ -498,6 +492,7 @@ dt_probe_tag(dt_probe_t *prp, uint_t argn, dt_node_t *dnp) tag, ctf_errmsg(ctf_errno(dtt.dtt_ctfp))); } } +free (tag);memset (dnp,0 ,sizeof (dt_node_t)); dnp->dn_kind =DT_NODE_TYPE ;
libdtrace/dt_prov_dtrace.c +3 -9
@@ -232,7 +232,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 */
@@ -252,16 +251,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 ;
Signature · 1 line
2.47.3
Quoted 88 lines · On Fri, Jan 16, 2026 at 01:55:29PM -0500, [email protected] wrote:
From: Eugene Loh <[email protected]>
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.Signed-off-by Eugene Loh <[email protected]>--- libdtrace/dt_link.c | 17 +++++++---------- libdtrace/dt_probe.c | 11 +++-------- libdtrace/dt_prov_dtrace.c | 12 +++--------- 3 files changed, 13 insertions(+), 27 deletions(-)libdtrace/dt_link.c +7
-10@@ -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));libdtrace/dt_probe.c +1
-2@@ -1,6 +1,6 @@/* * Oracle Linux DTrace. -* Copyright (c) 2006, 2025, Oracle and/or its affiliates. All rights reserved. +* Copyright (c) 2006, 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. */ @@ -475,15 +475,9 @@ dt_probe_tag(dt_probe_t *prp, uint_t argn, dt_node_t *dnp) {dtrace_hdl_t *dtp = prp->prov->pv_hdl;dtrace_typeinfo_t dtt; -size_t len;
libdtrace/dt_probe.c dt_probe_tag(dt_probe_t *prp, uint_t argn, dt_node_t *dnp) · line 481
Quoted 10 lines
libdtrace/dt_probe.c +1
-6char *tag; - len =snprintf (NULL ,0 ,"__dtrace_%s___%s_arg%u" , - prp->prov->desc.dtvd_name, prp->pr_name, argn); - - tag =alloca (len +1 ); - -snprintf (tag, len +1 ,"__dtrace_%s___%s_arg%u" , +asprintf (&tag,"__dtrace_%s___%s_arg%u" , prp->prov->desc.dtvd_name, prp->pr_name, argn);
This one should not be replaced, because if the ctf_add_typedef fails, 'tag' will never be freed and thus becomes a memory leak. Granted, in that case dtrace will end with an error message anyway, but still, it is a bit sloppy.
This is why the original code here is using an alloca()... that goes away at the end of the function scope.
Quoted 45 lines
libdtrace/dt_probe.c +1
-0if (dtrace_lookup_by_type (dtp,DTRACE_OBJ_DDEFS , tag, &dtt) !=0 ) {@@ -498,6 +492,7 @@ dt_probe_tag(dt_probe_t *prp, uint_t argn, dt_node_t *dnp) tag, ctf_errmsg(ctf_errno(dtt.dtt_ctfp))); } } +free (tag);memset (dnp,0 ,sizeof (dt_node_t)); dnp->dn_kind =DT_NODE_TYPE ;libdtrace/dt_prov_dtrace.c +3
-9@@ -232,7 +232,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 */ @@ -252,16 +251,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 ;Signature · 1 line
2.47.3