[Libguestfs] [nbdkit PATCH 3/5] api: Add nbdkit_string_intern helper

Eric Blake eblake at redhat.com
Wed Aug 26 13:33:39 UTC 2020


On 8/25/20 3:32 PM, Eric Blake wrote:

>>> -      keys[optind] = strndup (argv[optind], n);
>>> -      if (keys[optind] == NULL) {
>>> -        perror ("strndup");
>>> +      CLEANUP_FREE char *key = strndup (argv[optind], n);
>>> +      const char *safekey = nbdkit_string_intern (key);
>>> +      if (safekey == NULL)
>>>           exit (EXIT_FAILURE);
>>
>> It seems it might also be nice to have a "strndup" version of the
>> intern function.
> 
> Or even a memdup.  Yeah, those are possibilities as well.  Doing all 
> three (strdup, strndup, memdup) up front isn't hard.  If API 
> proliferation is a problem, you can combine strdup and strndup by 
> accepting -1 as a length to mean stop at NUL termination; but I'm less 
> worried about API proliferation and more about ease-of-use.

Time for some name bike-shedding.  How about:

nbdkit_strdup_intern (const char *)
nbdkit_strndup_intern (const char *s, size_t)

and if we need something that permits embedded NUL (right now I don't 
have such a use) we could add
nbdkit_memdup_intern (const void *s, size_t)

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org




More information about the Libguestfs mailing list