qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* Better alternative to strncpy in QEMU.
@ 2021-04-11 13:50 Chetan
  2021-04-12  4:51 ` Thomas Huth
  2021-04-12 13:19 ` Peter Maydell
  0 siblings, 2 replies; 6+ messages in thread
From: Chetan @ 2021-04-11 13:50 UTC (permalink / raw)
  To: qemu-devel, Thomas Huth

[-- Attachment #1: Type: text/plain, Size: 2814 bytes --]

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. 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){    /* 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;}/* 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;} *

Regards,
Chetan P.

[-- Attachment #2: Type: text/html, Size: 3356 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread
[parent not found: <mailman.36964.1618210428.30242.qemu-devel@nongnu.org>]

end of thread, other threads:[~2021-04-13  7:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-11 13:50 Better alternative to strncpy in QEMU Chetan
2021-04-12  4:51 ` Thomas Huth
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).