All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: Chetan <chetan4windows@gmail.com>, qemu-devel@nongnu.org
Subject: Re: Better alternative to strncpy in QEMU.
Date: Mon, 12 Apr 2021 06:51:41 +0200	[thread overview]
Message-ID: <162f832d-ea91-a8f4-6f1d-56cda086f481@redhat.com> (raw)
In-Reply-To: <CAPPKfOGwK7JDfHaTT-e4Z7bFkYoWu=dHvF-fT+QdqJhnwCLvOw@mail.gmail.com>

On 11/04/2021 15.50, Chetan wrote:
> Hello All,
> 
> This mail is in reference to one of the tasks mentioned in 
> '/Contribute/BiteSizedTasks/' in QEMU wiki, under '/API conversion/' which 
> states to introduce a better alternative to strncpy function.

Looks like this task has been added by Paolo, so I'm adding him to Cc: now.

( 
https://wiki.qemu.org/index.php?title=Contribute/BiteSizedTasks&diff=9130&oldid=9045 
)

> I've drafted 
> and tested below implementation for the same. Before proceeding with any 
> changes in QEMU code can you all please go through it and suggest 
> changes/corrections if required.
> 
> //* This function is introduced in place of strncpy(), it asserts if destination
>   * is large enough to fit strlen(source)+1 bytes and guarantees null 
> termination
>   * in destination string.
>   *
>   * char source[], is expecting a pointer to the source where data should be 
> copied
>   * from.
>   *
>   * char destination[], is expecting a pointer to the destination where data 
> should
>   * be copied to.
>   *
>   * size_t destination_size, is expecting size of destination.
>   * In case of char[], sizeof() function can be used to find the size.
>   * In case of char *, provide value which was passed to malloc() function for
>   * memory allocation.
>   */
> char *qemu_strncpy(char destination[], char source[], size_t destination_size)

Please use "*destination" and "*source" instead of "destination[]" and 
"source[]" here.

> {
>      /* Looping through the array and copying the characters from
>       * source to destination.
>       */
>      for (int i = 0; i < strlen(source); i++) {
>          destination[i] = source[i];
> 
>          /* Check if value of i is equal to the second last index
>           * of destination array and if condition is true, mark last
>           * index as NULL and break from the loop.
>           */
>          if (i == (destination_size - 2)) {
>              destination[destination_size - 1] = '\0';
>              break;
>          }
>      }
>      return destination;
> }

I think this is pretty much the same as g_strlcpy() from the glib:

https://developer.gnome.org/glib/2.66/glib-String-Utility-Functions.html#g-strlcpy

So I guess Paolo had something different in mind when adding this task?

> /* This function is introduced in place of strncpy(), it asserts if destination
>   * is large enough to fit strlen(source) bytes and does not guarantee null
>   * termination in destination string.
>   *
>   * char source[], is expecting a pointer to the source where data should be 
> copied
>   * from.
>   *
>   * char destination[], is expecting a pointer to the destination where data 
> should
>   * be copied to.
>   *
>   * size_t destination_size, is expecting size of destination.
>   * In case of char[], sizeof() function can be used to find the size.
>   * In case of char *, provide value which was passed to malloc() function for
>   * memory allocation.
>   */
> char *qemu_strncpy_nonul(char destination[], char source[], size_t 
> destination_size)
> {
>      /* Looping through the array and copying the characters from
>       * source to destination.
>       */
>      for (int i = 0; i < strlen(source); i++) {
>          destination[i] = source[i];
> 
>          /* Check if value of i is equal to the last index
>           * of the destination array and if condition is true,
>           * break from the loop.
>           */
>          if (i == (destination_size - 1)) {
>              break;
>          }
>      }
>      return destination;
> } /

I'm not sure what's the improvement over strncpy() here? Paolo, could you 
elaborate?
(Note that we also have some functions like strpadcpy() in QEMU already, 
which can be used in similar ways)

  Thomas



  reply	other threads:[~2021-04-12  4:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-11 13:50 Better alternative to strncpy in QEMU Chetan
2021-04-12  4:51 ` Thomas Huth [this message]
2021-04-13  7:32   ` Paolo Bonzini
2021-04-12 13:19 ` Peter Maydell
2021-04-13  2:50   ` Chetan
     [not found] <mailman.36964.1618210428.30242.qemu-devel@nongnu.org>
2021-04-12 12:48 ` Bruno Piazera Larsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=162f832d-ea91-a8f4-6f1d-56cda086f481@redhat.com \
    --to=thuth@redhat.com \
    --cc=chetan4windows@gmail.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.