[DRMAA-WG] C binding -- string lengths

Piotr Domagalski piotr.domagalski at man.poznan.pl
Thu Oct 23 10:29:28 CDT 2008


Hi,

As I've been lately looking at some example DRMAA code, e.g. the one
in the testsuite, it appeared to me that the size/length of buffer in
C binding is still a bit messy...

Almost all the example codes look like this:

char buf[DRMAA_ERROR_STRING_BUFFER];
drmaa_init(NULL, buf, sizeof(buf) - 1);

The third parameter is called err_diag_len. From DRMAA C Binding:

error_diagnosis – A buffer into which error diagnosis information will
be written.
error_diag_len – The size in characters of the error diagnosis string buffer.

So if error_diag_len is supposed to mean size of the string buffer why
sizeof(buf) is not used? Shouldn't it be called error_diag_size to be
less confusing?

I remember Hrabri saying that sizeof(buf) - 1 is to be used because
that is what programmers are used to. And I cannot agree with that.
This is what they use e.g. with strncpy() and it causes a *lot* of
confusion and errors (just man strncpy). Take a look at snprintf or
strftime which are supposed to get sizeof(buf), or strlcat/strlcpy
(not available in glibc) and it makes life a lot simpler -- you just
supply the total size of the buffer and don't worry about the output
being nul-terminated or not (it always is, even when the string is
truncated).

I know it's a little detail but I like things being consistent :)

-- 
Piotr Domagalski


More information about the drmaa-wg mailing list