diff mbox series

[9/9] target/i386: Replace sprintf() by snprintf()

Message ID 20240411104340.6617-10-philmd@linaro.org (mailing list archive)
State New, archived
Headers show
Series None | expand

Commit Message

Philippe Mathieu-Daudé April 11, 2024, 10:43 a.m. UTC
sprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1,
resulting in painful developper experience. Use snprintf() instead.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 target/i386/kvm/kvm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Peter Maydell April 11, 2024, 11:59 a.m. UTC | #1
On Thu, 11 Apr 2024 at 11:44, Philippe Mathieu-Daudé <philmd@linaro.org> wrote:
>
> sprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1,
> resulting in painful developper experience. Use snprintf() instead.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>  target/i386/kvm/kvm.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
> index e68cbe9293..a46d1426bf 100644
> --- a/target/i386/kvm/kvm.c
> +++ b/target/i386/kvm/kvm.c
> @@ -5335,7 +5335,8 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
>      case KVM_EXIT_NOTIFY:
>          ctx_invalid = !!(run->notify.flags & KVM_NOTIFY_CONTEXT_INVALID);
>          state = KVM_STATE(current_accel());
> -        sprintf(str, "Encounter a notify exit with %svalid context in"
> +        snprintf(str, sizeof(str),
> +                     "Encounter a notify exit with %svalid context in"
>                       " guest. There can be possible misbehaves in guest."
>                       " Please have a look.", ctx_invalid ? "in" : "");
>          if (ctx_invalid ||
> --

This is a case where I think we would be better off with
g_strdup_printf():

 * the buffer declaration is a long way away from its use
 * the string is long and it's not trivial to confirm that
   it will fit in the buffer
 * it's quite plausible somebody will come along later to
   clean up the wording of the error message and not notice
   they need to enlarge the buffer
 * it's only for printing a warning, so it's not going to
   be in a hot codepath

thanks
-- PMM
Richard Henderson April 11, 2024, 9:46 p.m. UTC | #2
On 4/11/24 03:43, Philippe Mathieu-Daudé wrote:
> sprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1,
> resulting in painful developper experience. Use snprintf() instead.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   target/i386/kvm/kvm.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
> index e68cbe9293..a46d1426bf 100644
> --- a/target/i386/kvm/kvm.c
> +++ b/target/i386/kvm/kvm.c
> @@ -5335,7 +5335,8 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
>       case KVM_EXIT_NOTIFY:
>           ctx_invalid = !!(run->notify.flags & KVM_NOTIFY_CONTEXT_INVALID);
>           state = KVM_STATE(current_accel());
> -        sprintf(str, "Encounter a notify exit with %svalid context in"
> +        snprintf(str, sizeof(str),
> +                     "Encounter a notify exit with %svalid context in"
>                        " guest. There can be possible misbehaves in guest."
>                        " Please have a look.", ctx_invalid ? "in" : "");
>           if (ctx_invalid ||

In the larger context,

>         if (ctx_invalid ||
>             state->notify_vmexit == NOTIFY_VMEXIT_OPTION_INTERNAL_ERROR) {
>             warn_report("KVM internal error: %s", str);
>             ret = -1;
>         } else {
>             warn_report_once("KVM: %s", str);
>             ret = 0;
>         }

so there's really no need to sprintf into a buffer at all -- just pass it all to 
warn_report_*.

The English text could use some improvement as well.  :-)


r~
diff mbox series

Patch

diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index e68cbe9293..a46d1426bf 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -5335,7 +5335,8 @@  int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
     case KVM_EXIT_NOTIFY:
         ctx_invalid = !!(run->notify.flags & KVM_NOTIFY_CONTEXT_INVALID);
         state = KVM_STATE(current_accel());
-        sprintf(str, "Encounter a notify exit with %svalid context in"
+        snprintf(str, sizeof(str),
+                     "Encounter a notify exit with %svalid context in"
                      " guest. There can be possible misbehaves in guest."
                      " Please have a look.", ctx_invalid ? "in" : "");
         if (ctx_invalid ||