You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@subversion.apache.org by st...@apache.org on 2014/10/17 19:35:47 UTC

svn commit: r1632638 - in /subversion/trunk/subversion: include/svn_string.h libsvn_subr/string.c

Author: stefan2
Date: Fri Oct 17 17:35:46 2014
New Revision: 1632638

URL: http://svn.apache.org/r1632638
Log:
Follow-up to r1632530: Treat all C string source buffers of known length
equally in our string APIs.  If the length is given as 0, allow for the
source buffer to be NULL.

* subversion/include/svn_string.h
  (svn_string_create_empty,
   svn_stringbuf_create,
   svn_stringbuf_appendcstr,
   svn_stringbuf_remove): Update docstrings.

* subversion/libsvn_subr/string.c
  (svn_string_ncreate,
   svn_stringbuf_ncreate,
   svn_stringbuf_insert,
   svn_stringbuf_replace): Handle 0 length buffers explicitly and prevent
                           access to the source buffers in that case.

Modified:
    subversion/trunk/subversion/include/svn_string.h
    subversion/trunk/subversion/libsvn_subr/string.c

Modified: subversion/trunk/subversion/include/svn_string.h
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/include/svn_string.h?rev=1632638&r1=1632637&r2=1632638&view=diff
==============================================================================
--- subversion/trunk/subversion/include/svn_string.h (original)
+++ subversion/trunk/subversion/include/svn_string.h Fri Oct 17 17:35:46 2014
@@ -140,6 +140,8 @@ svn_string_create_empty(apr_pool_t *pool
 /** Create a new string copied from a generic string of bytes, @a bytes, of
  * length @a size bytes.  @a bytes is NOT assumed to be null-terminated, but
  * the new string will be.
+ *
+ * @since Since 1.9, @a bytes can be NULL if @a size is zero.
  */
 svn_string_t *
 svn_string_ncreate(const char *bytes, apr_size_t size, apr_pool_t *pool);
@@ -205,6 +207,8 @@ svn_stringbuf_create(const char *cstring
 /** Create a new stringbuf copied from the generic string of bytes, @a bytes,
  * of length @a size bytes.  @a bytes is NOT assumed to be null-terminated,
  * but the new stringbuf will be.
+ *
+ * @since Since 1.9, @a bytes can be NULL if @a size is zero.
  */
 svn_stringbuf_t *
 svn_stringbuf_ncreate(const char *bytes, apr_size_t size, apr_pool_t *pool);
@@ -351,6 +355,8 @@ svn_stringbuf_appendcstr(svn_stringbuf_t
  * @note The inserted string may be a sub-range if @a str.
  *
  * @since New in 1.8.
+ *
+ * @since Since 1.9, @a bytes can be NULL if @a count is zero.
  */
 void
 svn_stringbuf_insert(svn_stringbuf_t *str,
@@ -382,6 +388,8 @@ svn_stringbuf_remove(svn_stringbuf_t *st
  * @endcode
  *
  * @since New in 1.8.
+ *
+ * @since Since 1.9, @a bytes can be NULL if @a new_count is zero.
  */
 void
 svn_stringbuf_replace(svn_stringbuf_t *str,

Modified: subversion/trunk/subversion/libsvn_subr/string.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/string.c?rev=1632638&r1=1632637&r2=1632638&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/string.c (original)
+++ subversion/trunk/subversion/libsvn_subr/string.c Fri Oct 17 17:35:46 2014
@@ -26,6 +26,7 @@
 
 
 #include <apr.h>
+#include <assert.h>
 
 #include <string.h>      /* for memcpy(), memcmp(), strlen() */
 #include <apr_fnmatch.h>
@@ -239,7 +240,9 @@ svn_string_ncreate(const char *bytes, ap
   new_string->data = data;
   new_string->len = size;
 
-  memcpy(data, bytes, size);
+  /* If SIZE is 0, NULL is valid for BYTES. */
+  if (size)
+    memcpy(data, bytes, size);
 
   /* Null termination is the convention -- even if we suspect the data
      to be binary, it's not up to us to decide, it's the caller's
@@ -392,7 +395,10 @@ svn_stringbuf_t *
 svn_stringbuf_ncreate(const char *bytes, apr_size_t size, apr_pool_t *pool)
 {
   svn_stringbuf_t *strbuf = svn_stringbuf_create_ensure(size, pool);
-  memcpy(strbuf->data, bytes, size);
+
+  /* If SIZE is 0, NULL is valid for BYTES. */
+  if (size)
+    memcpy(strbuf->data, bytes, size);
 
   /* Null termination is the convention -- even if we suspect the data
      to be binary, it's not up to us to decide, it's the caller's
@@ -645,6 +651,10 @@ svn_stringbuf_insert(svn_stringbuf_t *st
                      const char *bytes,
                      apr_size_t count)
 {
+  /* For COUNT==0, we allow BYTES to be NULL. It's a no-op in that case. */
+  if (count == 0)
+    return;
+
   if (bytes + count > str->data && bytes < str->data + str->blocksize)
     {
       /* special case: BYTES overlaps with this string -> copy the source */
@@ -685,6 +695,14 @@ svn_stringbuf_replace(svn_stringbuf_t *s
                       const char *bytes,
                       apr_size_t new_count)
 {
+  /* For COUNT==0, we allow BYTES to be NULL.
+   * In that case, this is just a substring removal. */
+  if (new_count == 0)
+    {
+      svn_stringbuf_remove(str, pos, old_count);
+      return;
+    }
+
   if (bytes + new_count > str->data && bytes < str->data + str->blocksize)
     {
       /* special case: BYTES overlaps with this string -> copy the source */