Hi,

Even if Apple are incorrectly deprecating sprintf() and even if we know
that our string will fit in the allocated buffer... Is there a significant
drawback of just using snprintf()?

[[[
Index: subversion/svn/filesize.c
===================================================================
--- subversion/svn/filesize.c   (revision 1934787)
+++ subversion/svn/filesize.c   (working copy)
@@ -97,13 +97,13 @@
        the absolute size is actually a single-digit number, because
        files can't have fractional byte sizes. */
     if (absolute_human_readable_size >= 10)
-      sprintf(buffer, "%.0f", human_readable_size);
+      snprintf(buffer, sizeof(buffer), "%.0f", human_readable_size);
     else
       {
         double integral;
         const double frac = modf(absolute_human_readable_size, &integral);
         const int decimals = (index > 0 && (integral < 9 || frac <=
.949999999));
-        sprintf(buffer, "%.*f", decimals, human_readable_size);
+        snprintf(buffer, sizeof(buffer), "%.*f", decimals,
human_readable_size);
       }

     return apr_pstrcat(result_pool, buffer, suffix, SVN_VA_NULL);
]]]

Kind regards,
Daniel


Den lör 30 maj 2026 kl 23:00 skrev <[email protected]>:

> Author: brane
> Date: Sat May 30 21:00:08 2026
> New Revision: 1934788
>
> Log:
> Silence Apple's deprecation of sprintf().
>
> * subversion/svn/filesize.c
>   (format_size): Wrap the function implementation in diagnostic #pragma
>    to silence [-Wdeprecated-declarations].
>
> * subversion/tests/libsvn_subr/skel-test.c
>   (put_explicit_length): Likewise.
>
> Modified:
>    subversion/trunk/subversion/svn/filesize.c
>    subversion/trunk/subversion/tests/libsvn_subr/skel-test.c
>
> Modified: subversion/trunk/subversion/svn/filesize.c
>
> ==============================================================================
> --- subversion/trunk/subversion/svn/filesize.c  Sat May 30 20:40:53 2026
>       (r1934787)
> +++ subversion/trunk/subversion/svn/filesize.c  Sat May 30 21:00:08 2026
>       (r1934788)
> @@ -72,6 +72,16 @@ format_size(double human_readable_size,
>              apr_size_t index,
>              apr_pool_t *result_pool)
>  {
> +  /* Apple in its infinite wisdom has seen fit to deprecate sprintf()
> which
> +     has been part of the C standard library since the K&R days and is not
> +     deprecated in any version of the C standard. */
> +#ifdef __APPLE__
> +#  if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
> +#    pragma GCC diagnostic push
> +#    pragma GCC diagnostic ignored "-Wdeprecated-declarations"
> +#  endif
> +#endif /* __APPLE__ */
> +
>    /* NOTE: We want to display a locale-specific decimal sepratator, but
>             APR's formatter completely ignores the locale. So we use the
>             good, old, standard, *dangerous* sprintf() to format the size.
> @@ -107,6 +117,12 @@ format_size(double human_readable_size,
>        }
>
>      return apr_pstrcat(result_pool, buffer, suffix, SVN_VA_NULL);
> +
> +#ifdef __APPLE__
> +#  if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
> +#    pragma GCC diagnostic pop
> +#  endif
> +#endif /* __APPLE__ */
>  }
>
>
>
> Modified: subversion/trunk/subversion/tests/libsvn_subr/skel-test.c
>
> ==============================================================================
> --- subversion/trunk/subversion/tests/libsvn_subr/skel-test.c   Sat May 30
> 20:40:53 2026        (r1934787)
> +++ subversion/trunk/subversion/tests/libsvn_subr/skel-test.c   Sat May 30
> 21:00:08 2026        (r1934788)
> @@ -307,6 +307,16 @@ put_explicit_length(svn_stringbuf_t *str
>                      apr_size_t len,
>                      char sep)
>  {
> +  /* Apple in its infinite wisdom has seen fit to deprecate sprintf()
> which
> +     has been part of the C standard library since the K&R days and is not
> +     deprecated in any version of the C standard. */
> +#ifdef __APPLE__
> +#  if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
> +#    pragma GCC diagnostic push
> +#    pragma GCC diagnostic ignored "-Wdeprecated-declarations"
> +#  endif
> +#endif /* __APPLE__ */
> +
>    char *buf = malloc(len + 100);
>    apr_size_t length_len;
>
> @@ -322,6 +332,12 @@ put_explicit_length(svn_stringbuf_t *str
>
>    svn_stringbuf_appendbytes(str, buf, length_len + len);
>    free(buf);
> +
> +#ifdef __APPLE__
> +#  if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
> +#    pragma GCC diagnostic pop
> +#  endif
> +#endif /* __APPLE__ */
>  }
>
>
>
>

Reply via email to