[DRMAA-WG] C binding -- string lengths

Roger Brobst rogerb at cadence.com
Thu Oct 23 12:49:52 CDT 2008


> it appeared to me that the size/length of buffer 
> in C binding is still a bit messy...

I would agree that the phrase "size/length of buffer" 
is confusing.

I would hope the document would either indicate the
size of the buffer, or the length of the string which
the buffer can accomodate.

> 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?

Yes, *IF* the intent was to accept the buffer size, then
the variable error_diag_size would be a better choice.
Since that's not the intent, it wasn't chosen.


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

Tricky to decide where to be consistent.
This business about needing an extra byte to terminate a string
is not particularly consistent between languages, and the
C-lang DRMAA binding is most certainly leveraged by other
languages.

-Roger


----Original Message----
From: "Piotr Domagalski" <piotr.domagalski at man.poznan.pl>
Sender: drmaa-wg-bounces at ogf.org
To: "DRMAA Working Group" <drmaa-wg at gridforum.org>
Subject: [DRMAA-WG] C binding -- string lengths
Date: Thu, 23 Oct 2008 17:29:28 +0200

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
--
  drmaa-wg mailing list
  drmaa-wg at ogf.org
  http://www.ogf.org/mailman/listinfo/drmaa-wg


More information about the drmaa-wg mailing list