Skip to contentSkip to conversation

Fix calling convention of huri_field_escape

PATCH Applied Pete Zaitcev 2 participants 2 messages Sep 28, 2010 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix calling convention of huri_field_escape

Premature optimization is the root of all evil.

Use a sensible convention of not screwing with the argument, at the expense of extra strdup.

Fortunately, all users are confined to Hail itself, even if huri_field_escape is exported.

---
 include/hstor.h |    2 +-
 lib/hstor.c     |   44 +++++++++++++++++++++++++++++---------------
 lib/huri.c      |   10 +++++-----
 3 files changed, 35 insertions(+), 21 deletions(-)
include/hstor.h +1 -1
index cd04c56..8620d3b 100644
@@ -144,7 +144,7 @@ extern int hreq_acl_canned(struct http_req *req);
 /* uri.c */
 extern struct http_uri *huri_parse(struct http_uri *uri_dest, char *uri_src_text);
 extern int huri_field_unescape(char *s, int s_len);
-extern char* huri_field_escape (char *signed_str, unsigned char mask);
+extern char* huri_field_escape(const char *signed_str, unsigned char mask);
 
 static inline bool hreq_http11(struct http_req *req)
 {
lib/hstor.c +29 -15

126 lines not on this page · raw message

lib/huri.c +5 -5
index 7198536..35d857e 100644
@@ -25,7 +25,6 @@
 #include <string.h>
 #include <ctype.h>
 #include <stdbool.h>
-#include <glib.h>
 #include <hstor.h>
 
 /* our own ISSPACE.  ANSI isspace is locale dependent */
@@ -221,7 +220,7 @@ static const guchar neednt_escape_table[] =
 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
 };
 
-char* huri_field_escape (char *signed_str, unsigned char mask)
+char* huri_field_escape(const char *signed_str, unsigned char mask)
 {
   int len;
   int i;
@@ -250,10 +249,12 @@ char* huri_field_escape (char *signed_str, unsigned char mask)
 
   /* Don't escape if unnecessary */
   if (must_escape == FALSE)
-    return signed_str;
+    return strdup(signed_str);
 
   /* Allocate buffer */
-  dst = (gchar*) g_malloc(len + 1);
+  dst = malloc(len + 1);
+  if (!dst)
+    return NULL;
 
   /* Copy */
   for (i = j = 0; str[i]; i++, j++)
@@ -284,7 +285,6 @@ char* huri_field_escape (char *signed_str, unsigned char mask)
     }
   dst[j] = '\0';
 
-  g_free (signed_str);
   return dst;
 }
 
Applied
#1 lore raw
Quoted 8 lines · Pete Zaitcev

Premature optimization is the root of all evil.

Use a sensible convention of not screwing with the argument, at the expense of extra strdup.

Fortunately, all users are confined to Hail itself, even if huri_field_escape is exported.

Quoted 7 lines
---
  include/hstor.h |    2 +-
  lib/hstor.c     |   44 +++++++++++++++++++++++++++++---------------
  lib/huri.c      |   10 +++++-----
  3 files changed, 35 insertions(+), 21 deletions(-)

applied