[cairo] [PATCH] boilerplate: Add xsprintf() and xsnprintf()

RAVI NANJUNDAPPA nravi.n at samsung.com
Tue Oct 21 22:13:43 PDT 2014


(formatting was the problem with the earlier mail. Pls ignore that mail.)
Agreed. But we need to check for other conditions like if the user is passing NULL string value or not (as it's been done in xstrdup())
In which case also, we need to exit from the execution environment.

Thanks and Best Regards, 
N Ravi
------- Original Message -------
Sender : Bill Spitzak<spitzak at gmail.com>
Date : Oct 22, 2014 01:30 (GMT+05:30)
Title : Re: [cairo] [PATCH] boilerplate: Add xsprintf() and xsnprintf()

According to the man page "If an output error is encountered, a negative 
value is returned." It seems very unlikely that out of memory will be 
the cause of the error. In fact I suspect the s*printf functions never 
cause memory to be allocated so this is never how they fail.

Also if you are going to do this, only provide the save snprintf and 
hide sprintf.

On 10/21/2014 02:06 AM, Ravi Nanjundappa wrote:
> Add xsprintf() and xsnprintf() functions to boilerplate code
> and replace sprintf() and snprintf() accordingly wherever applicable.
>
> Signed-off-by: Ravi Nanjundappa 
> ---
>   boilerplate/cairo-boilerplate-pdf.c            |    2 +-
>   boilerplate/cairo-boilerplate-ps.c             |    2 +-
>   boilerplate/cairo-boilerplate-svg.c            |    2 +-
>   boilerplate/cairo-boilerplate-system.c         |   51 ++++++++++++++++++++++++
>   boilerplate/cairo-boilerplate-system.h         |   14 +++++++
>   boilerplate/cairo-boilerplate-win32-printing.c |    2 +-
>   boilerplate/cairo-boilerplate.c                |    4 +-
>   perf/cairo-perf-chart.c                        |   14 +++----
>   test/cairo-test-runner.c                       |    2 +-
>   test/pdf-mime-data.c                           |    2 +-
>   util/cairo-sphinx/sphinx.c                     |   22 +++++-----
>   util/xml-to-trace.c                            |    2 +-
>   12 files changed, 92 insertions(+), 27 deletions(-)
>
> diff --git a/boilerplate/cairo-boilerplate-pdf.c b/boilerplate/cairo-boilerplate-pdf.c
> index d76d139..a090a2a 100644
> --- a/boilerplate/cairo-boilerplate-pdf.c
> +++ b/boilerplate/cairo-boilerplate-pdf.c
> @@ -166,7 +166,7 @@ _cairo_boilerplate_pdf_surface_write_to_png (cairo_surface_t *surface,
>       char    command[4096];
>       int exitstatus;
>
> -    sprintf (command, "./pdf2png %s %s 1",
> +    xsprintf (command, "./pdf2png %s %s 1",
>        ptc->filename, filename);
>
>       exitstatus = system (command);
> diff --git a/boilerplate/cairo-boilerplate-ps.c b/boilerplate/cairo-boilerplate-ps.c
> index ae61239..f0c9055 100644
> --- a/boilerplate/cairo-boilerplate-ps.c
> +++ b/boilerplate/cairo-boilerplate-ps.c
> @@ -221,7 +221,7 @@ _cairo_boilerplate_ps_surface_write_to_png (cairo_surface_t *surface,
>       char    command[4096];
>       int exitstatus;
>
> -    sprintf (command, "gs -q -r72 -g%dx%d -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=%s %s %s",
> +    xsprintf (command, "gs -q -r72 -g%dx%d -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=%s %s %s",
>        ptc->width, ptc->height, filename,
>        ptc->level == CAIRO_PS_LEVEL_2 ? "-c 2 .setlanguagelevel -f" : "",
>        ptc->filename);
> diff --git a/boilerplate/cairo-boilerplate-svg.c b/boilerplate/cairo-boilerplate-svg.c
> index 797106e..92d856d 100644
> --- a/boilerplate/cairo-boilerplate-svg.c
> +++ b/boilerplate/cairo-boilerplate-svg.c
> @@ -198,7 +198,7 @@ _cairo_boilerplate_svg_surface_write_to_png (cairo_surface_t *surface,
>       char    command[4096];
>       int exitstatus;
>
> -    sprintf (command, "./svg2png %s %s",
> +    xsprintf (command, "./svg2png %s %s",
>        ptc->filename, filename);
>
>       exitstatus = system (command);
> diff --git a/boilerplate/cairo-boilerplate-system.c b/boilerplate/cairo-boilerplate-system.c
> index ec23341..36dc85f 100644
> --- a/boilerplate/cairo-boilerplate-system.c
> +++ b/boilerplate/cairo-boilerplate-system.c
> @@ -140,6 +140,57 @@ xasprintf (char       **strp,
>   #endif /* !HAVE_VASNPRINTF */
>   }
>
> +int
> +xsprintf (char       *str,
> +    const char  *fmt,
> + ...)
> +{
> +    va_list va;
> +    int ret;
> +
> +    if (str == NULL) {
> + fprintf (stderr, "Error: string is null\n");
> + exit (1);
> +    }
> +
> +    va_start (va, fmt);
> +    ret = vsprintf(str, fmt, va);
> +    va_end (va);
> +
> +    if (ret < 0) {
> + fprintf (stderr, "Error: Out of memory. Exiting.\n");
> + exit (1);
> +    }
> +
> +    return ret;
> +}
> +
> +int
> +xsnprintf (char       *str,
> +    size_t  size,
> +    const char  *fmt,
> + ...)
> +{
> +    va_list va;
> +    int ret;
> +
> +    if (str == NULL) {
> + fprintf (stderr, "Error: string is null\n");
> + exit (1);
> +    }
> +
> +    va_start (va, fmt);
> +    ret = vsnprintf (str, size, fmt, va);
> +    va_end (va);
> +
> +    if (ret < 0) {
> + fprintf (stderr, "Error: Out of memory. Exiting.\n");
> + exit (1);
> +    }
> +
> +    return ret;
> +}
> +
>   void
>   xunlink (const char *pathname)
>   {
> diff --git a/boilerplate/cairo-boilerplate-system.h b/boilerplate/cairo-boilerplate-system.h
> index 2816567..7ea2a17 100644
> --- a/boilerplate/cairo-boilerplate-system.h
> +++ b/boilerplate/cairo-boilerplate-system.h
> @@ -48,6 +48,20 @@ xasprintf (char       **strp,
>      const char  *fmt,
>   ...) CAIRO_BOILERPLATE_PRINTF_FORMAT(2, 3);
>
> +#define xsprintf cairo_boilerplate_xsprintf
> +int
> +xsprintf (char       *strp,
> +    const char  *fmt,
> + ...) CAIRO_BOILERPLATE_PRINTF_FORMAT(2, 3);
> +
> +#define xsnprintf cairo_boilerplate_xsnprintf
> +int
> +xsnprintf (char       *strp,
> +    size_t  size,
> +    const char  *fmt,
> + ...) CAIRO_BOILERPLATE_PRINTF_FORMAT(3, 4);
> +
> +
>   #define xunlink cairo_boilerplate_xunlink
>   void
>   xunlink (const char *path);
> diff --git a/boilerplate/cairo-boilerplate-win32-printing.c b/boilerplate/cairo-boilerplate-win32-printing.c
> index 625d52c..5cebccd 100644
> --- a/boilerplate/cairo-boilerplate-win32-printing.c
> +++ b/boilerplate/cairo-boilerplate-win32-printing.c
> @@ -294,7 +294,7 @@ _cairo_boilerplate_win32_printing_surface_write_to_png (cairo_surface_t *surface
>       cairo_surface_finish (surface);
>       EndPage (ptc->dc);
>       EndDoc (ptc->dc);
> -    sprintf (command, "gs -q -r72 -g%dx%d -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=%s %s",
> +    xsprintf (command, "gs -q -r72 -g%dx%d -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=%s %s",
>        ptc->width + ptc->left_margin, ptc->height + ptc->bottom_margin, filename, ptc->filename);
>
>       if (system (command) != 0)
> diff --git a/boilerplate/cairo-boilerplate.c b/boilerplate/cairo-boilerplate.c
> index 7fdbf79..f4e07d6 100644
> --- a/boilerplate/cairo-boilerplate.c
> +++ b/boilerplate/cairo-boilerplate.c
> @@ -935,7 +935,7 @@ cairo_boilerplate_open_any2ppm (const char   *filename,
>   goto POPEN;
>       }
>
> -    len = sprintf (command, "%s %d\n", filename, page);
> +    len = xsprintf (command, "%s %d\n", filename, page);
>       if (write (sk, command, len) != len) {
>   close (sk);
>   goto POPEN;
> @@ -948,7 +948,7 @@ POPEN:
>   #endif
>
>       *close_cb = pclose;
> -    sprintf (command, "%s %s %d", any2ppm, filename, page);
> +    xsprintf (command, "%s %s %d", any2ppm, filename, page);
>       return popen (command, "rb");
>   }
>
> diff --git a/perf/cairo-perf-chart.c b/perf/cairo-perf-chart.c
> index 738fe5c..b249d72 100644
> --- a/perf/cairo-perf-chart.c
> +++ b/perf/cairo-perf-chart.c
> @@ -406,9 +406,9 @@ add_chart (struct chart *c,
>   cairo_set_font_size (c->cr, dx - 2);
>
>   if (value < 0) {
> -     sprintf (buf, "%.1f", -value/100 + 1);
> +     xsprintf (buf, "%.1f", -value/100 + 1);
>   } else {
> -     sprintf (buf, "%.1f", value/100 + 1);
> +     xsprintf (buf, "%.1f", value/100 + 1);
>   }
>   cairo_text_extents (c->cr, buf, &extents);
>
> @@ -516,9 +516,9 @@ add_average (struct chart *c,
>       cairo_set_font_size (c->cr, dx - 2);
>
>       if (value < 0) {
> - sprintf (buf, "%.1f", -value/100 + 1);
> + xsprintf (buf, "%.1f", -value/100 + 1);
>       } else {
> - sprintf (buf, "%.1f", value/100 + 1);
> + xsprintf (buf, "%.1f", value/100 + 1);
>       }
>       cairo_text_extents (c->cr, buf, &extents);
>
> @@ -643,7 +643,7 @@ done:
>
>   cairo_set_font_size (c->cr, 8);
>
> - sprintf (buf, "%.0fs", i*v/1000);
> + xsprintf (buf, "%.0fs", i*v/1000);
>   cairo_text_extents (c->cr, buf, &extents);
>
>   cairo_set_source_rgba (c->cr, .75, 0, 0, .95);
> @@ -704,7 +704,7 @@ done:
>   if (y > mid)
>       break;
>
> - sprintf (buf, "%.*fx", precision, i*v + 1);
> + xsprintf (buf, "%.*fx", precision, i*v + 1);
>   cairo_text_extents (c->cr, buf, &extents);
>
>   cairo_set_source_rgba (c->cr, .75, 0, 0, .95);
> @@ -986,7 +986,7 @@ add_legend (struct chart *chart)
>   str = chart->names[0] ?
>         chart->names[0] : chart->reports[0].configuration;
>
> - sprintf (buf, "(relative to %s)", str);
> + xsprintf (buf, "(relative to %s)", str);
>   cairo_text_extents (chart->cr, buf, &extents);
>
>   cairo_set_source_rgb (chart->cr, 1, 1, 1);
> diff --git a/test/cairo-test-runner.c b/test/cairo-test-runner.c
> index 3fb3ae5..221e042 100644
> --- a/test/cairo-test-runner.c
> +++ b/test/cairo-test-runner.c
> @@ -166,7 +166,7 @@ is_running_under_debugger (void)
>   #if HAVE_UNISTD_H && HAVE_LIBGEN_H && __linux__
>       char buf[1024];
>
> -    sprintf (buf, "/proc/%d/exe", getppid ());
> +    xsprintf (buf, "/proc/%d/exe", getppid ());
>       if (readlink (buf, buf, sizeof (buf)) != -1 &&
>   strncmp (basename (buf), "gdb", 3) == 0)
>       {
> diff --git a/test/pdf-mime-data.c b/test/pdf-mime-data.c
> index fd5af1f..fb9f8ab 100644
> --- a/test/pdf-mime-data.c
> +++ b/test/pdf-mime-data.c
> @@ -148,7 +148,7 @@ preamble (cairo_test_context_t *ctx)
>
>       printf ("pdf-mime-data: Please check %s to ensure it looks/prints correctly.\n", filename);
>
> -    sprintf (command, "pdfimages -j %s %s", filename, CAIRO_TEST_OUTPUT_DIR "/" BASENAME);
> +    xsprintf (command, "pdfimages -j %s %s", filename, CAIRO_TEST_OUTPUT_DIR "/" BASENAME);
>       exit_status = system (command);
>       free (filename);
>       if (exit_status) {
> diff --git a/util/cairo-sphinx/sphinx.c b/util/cairo-sphinx/sphinx.c
> index 3a6c04c..e181108 100644
> --- a/util/cairo-sphinx/sphinx.c
> +++ b/util/cairo-sphinx/sphinx.c
> @@ -354,7 +354,7 @@ clients_add_command (struct clients *clients, int fd, char *info)
>   c->reference[len] = '\0';
>       }
>
> -    len = sprintf (buf, "%s\n", clients->shm_path);
> +    len = xsprintf (buf, "%s\n", clients->shm_path);
>       writen (fd, buf, len);
>   }
>
> @@ -695,17 +695,17 @@ write_trace (struct clients *clients)
>
>       csum = checksum ("output/cairo-sphinx.trace");
>
> -    sprintf (buf, "output/%s.trace", csum);
> +    xsprintf (buf, "output/%s.trace", csum);
>       if (! g_file_test (buf, G_FILE_TEST_EXISTS)) {
>   rename ("output/cairo-sphinx.trace", buf);
>
> - sprintf (buf, "output/%s.recording.png", csum);
> + xsprintf (buf, "output/%s.recording.png", csum);
>   cairo_surface_write_to_png (clients->recording, buf);
>
>   for (i = 0; i < clients->count; i++) {
>       struct client_info *c = &clients->clients[i];
>       if (c->image != NULL) {
> - sprintf (buf, "output/%s.%s.png", csum, c->name);
> + xsprintf (buf, "output/%s.%s.png", csum, c->name);
>   cairo_surface_write_to_png (c->image, buf);
>       }
>   }
> @@ -849,7 +849,7 @@ request_image (struct client *c,
>
>       assert (format != CAIRO_FORMAT_INVALID);
>
> -    len = sprintf (buf, ".image %lu %d %d %d %d\n",
> +    len = xsprintf (buf, ".image %lu %d %d %d %d\n",
>      closure->id, format, width, height, stride);
>       writen (c->sk, buf, len);
>
> @@ -964,7 +964,7 @@ send_recording (struct client *c,
>       unsigned long serial;
>
>       assert (cairo_surface_get_type (source) == CAIRO_SURFACE_TYPE_RECORDING);
> -    len = sprintf (buf, ".recording %p %lu\n", source, closure->id);
> +    len = xsprintf (buf, ".recording %p %lu\n", source, closure->id);
>       writen (c->sk, buf, len);
>
>       /* wait for image check */
> @@ -1087,7 +1087,7 @@ recorder (void *arg)
>       buf_size = 65536;
>       buf = xmalloc (buf_size);
>
> -    len = sprintf (buf, "client-command target=recording name=.recorder\n");
> +    len = xsprintf (buf, "client-command target=recording name=.recorder\n");
>       if (! writen (client.sk, buf, len))
>   return NULL;
>
> @@ -1098,7 +1098,7 @@ recorder (void *arg)
>       if (pfd.fd < 0)
>   return NULL;
>
> -    len = sprintf (buf, "client-trace name=.recorder\n");
> +    len = xsprintf (buf, "client-trace name=.recorder\n");
>       if (! writen (pfd.fd, buf, len))
>   return NULL;
>
> @@ -1397,11 +1397,11 @@ do_client (int fd,
>       buf = xmalloc (buf_size);
>
>       if (reference != NULL) {
> - len = sprintf (buf,
> + len = xsprintf (buf,
>          "client-command name=%s target=%s reference=%s\n",
>          name, target, reference);
>       } else {
> - len = sprintf (buf,
> + len = xsprintf (buf,
>          "client-command name=%s target=%s\n",
>          name, target);
>       }
> @@ -1422,7 +1422,7 @@ do_client (int fd,
>       if (pfd.fd < 0)
>   return 1;
>
> -    len = sprintf (buf, "client-trace name=%s\n", name);
> +    len = xsprintf (buf, "client-trace name=%s\n", name);
>       if (! writen (pfd.fd, buf, len))
>   return 1;
>
> diff --git a/util/xml-to-trace.c b/util/xml-to-trace.c
> index 13b7e57..39e72a7 100644
> --- a/util/xml-to-trace.c
> +++ b/util/xml-to-trace.c
> @@ -166,7 +166,7 @@ start_element (void *closure,
>   }
>
>   fprintf (trace->stream, "[");
> - sprintf (trace->tail_buf, "] %s set-dash\n", offset);
> + xsprintf (trace->tail_buf, "] %s set-dash\n", offset);
>   trace->tail = trace->tail_buf;
>       } else {
>       }
>
-- 
cairo mailing list
cairo at cairographics.org
http://lists.cairographics.org/mailman/listinfo/cairo


More information about the cairo mailing list