C binding -- string lengths
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
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@man.poznan.pl> Sender: drmaa-wg-bounces@ogf.org To: "DRMAA Working Group" <drmaa-wg@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@ogf.org http://www.ogf.org/mailman/listinfo/drmaa-wg
On Thu, Oct 23, 2008 at 7:49 PM, Roger Brobst <rogerb@cadence.com> wrote:
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 was just wondering why the buffer size concept was not chosen. I think it's a lot simpler and less error-prone. But OK, I understand that the intent was to accept maximum string length. Now let's get back to the example: char buf[DRMAA_ERROR_STRING_BUFFER]; drmaa_init(NULL, buf, sizeof(buf) - 1); How would you implement the string copying in drmaa_init()? One would probably use the following code: int drmaa_init(..., char *err_diag, size_t err_diag_len) { strncpy(err_diag, SRC, err_diag_len) } Now what happens if strlen(SRC) >= sizeof(buf) - 1? Who is responsible for adding the terminating '\0'? -- Piotr Domagalski
How would you implement the string copying in drmaa_init()? One would probably use the following code:
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 ) }
On Fri, Oct 24, 2008 at 8:57 PM, Roger Brobst <rogerb@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 write: int 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 mean. -- Piotr Domagalski
participants (2)
-
Piotr Domagalski -
Roger Brobst