[redland-dev] Counted vs null-terminated strings

Victor Porton porton at narod.ru
Mon Aug 4 15:58:48 EDT 2014


The following function from Raptor 2.0.14 checks (in debug mode only) that a "counted" string is NUL-terminated.

I wonder: Aren't counted string here specifically to eliminate the need for '\0' at the end of a string?

Must I force strings to be nul-terminated even when they are "counted"?

I met with this problem while writing Ada bindings for Raptor. When I've noticed that it requires to be nul-terminated even counted string, I was tempted to remove all Append_Nul=>False for all Ada to C string conversion functions (for "counted" C functions). But removing it made things worse, as it changed the length of the generated arrays of "char" elements and broke my code.

So I need to carefully rewrite my Ada code to work with nul-terminated strings.

But what of the following two is an error?

1. Raptor checks (even in debug mode) that "counted" strings are properly nul-terminated.

2. I do not provide NUL-termination for counted strings.

raptor_uri*
raptor_new_uri_from_counted_string(raptor_world* world,
                                   const unsigned char *uri_string,
                                   size_t length)
{
  raptor_uri* new_uri;
  unsigned char *new_string;
  
  RAPTOR_CHECK_CONSTRUCTOR_WORLD(world);

  if(!uri_string || !*uri_string)
    return NULL;

  raptor_world_open(world);

#ifdef DEBUG
  RAPTOR_ASSERT(strlen((const char*)uri_string) != length,
                "URI string is not declared length");
#endif
/* ... */
}

--
Victor Porton - http://portonvictor.org


More information about the redland-dev mailing list