[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [libvirt] [PATCH] Introduce virStrncpy.



+  - virStrcpy(char *dest, const char *src, size_t destbytes)
+      Use this variant if you know you want to copy the entire src string
+      into dest.  This is equivalent to
+      virStrncpy(dest, src, strlen(src), destbytes)
+
+  - virStrcpyStatic(char *dest, const char *src)
+      Use this variant if you know you want to copy the entire src string
+      into dest *and* you know that your destination string is a static string
+      (i.e. that sizeof(dest) returns something meaningful).  This is
+      equivalent to virStrncpy(dest, src, strlen(src), sizeof(dest)).

I'm not familiar with the documentation style of libvirt, but it may be a good idea to say that these are not just equivalent: these are macros, and as such they may evaluate src multiple times.

For the same reason, here

+#define virStrcpy(dest, src, destbytes) virStrncpy(dest, src, strlen(src), destbytes) +#define virStrcpyStatic(dest, src) virStrncpy(dest, src, strlen(src), sizeof(dest))

it's better to parenthesize dest and src, or even to do something like

#define virStrcpyStatic(dest, src) virStrcpy((dest), (src), \
  sizeof ((dest)))

static inline char *
virStrcpy (char *dest, const char *src, int size)
{
    return virStrncpy (dest, src, strlen (src), size);
}

(I briefly thought about how to remove the strlen, but you have to do it anyway after the strncpy, so it's indeed easier to do it beforehand in virStrcpy, as you did).

Thanks,

Paolo


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]