[DRMAA-WG] C binding -- string lengths

Piotr Domagalski piotr.domagalski at man.poznan.pl
Sat Oct 25 16:47:15 CDT 2008

On Fri, Oct 24, 2008 at 8:57 PM, Roger Brobst <rogerb at cadence.com> wrote:
>> int
>> drmaa_init(..., char *err_diag, size_t err_diag_len)
>> {
>>   strncpy(err_diag, SRC, err_diag_len)
>> }
> I would not use use strncpy because
> if the string fits into the buffer,
>     I see no reason to zero-fill the remainder of the buffer;
> if the string doesn't fit,
>     the implementation should not copy an unterminated string
>     into the buffer.
> Assuming I wasn't doing any form of error checking, I would use
> int
> drmaa_init(..., char *err_diag, size_t err_diag_len)
> {
>    *err_diag = '\0', strncat( err_diag, SRC, err_diag_len )
> }

Yes, this is the safe way to "copy" the string into the buffer. In my
opinion the problem is that most programmers would use strncpy()
instead, simply because we're talking about copying, not
concatenating. With strncpy() the drmaa function is responsible for
terminating the string (in the worst case), so one would have to

drmaa_init(..., char *err_diag, size_t err_diag_len)
  strncpy(err_diag, SRC, err_diag_len);
  err_diag[err_diag_len] = '\0';

to be completely safe.

Anyway, I just wanted to bring this up so that this is commonly
understood by DRMAA implementators. And I wanted to be completely sure
that I understand what err_diag_len (and other *_len) is supposed to

Piotr Domagalski

More information about the drmaa-wg mailing list