Backported bug fix for #2956.
authorMichael Meskes <[email protected]>
Tue, 27 Feb 2007 13:27:13 +0000 (13:27 +0000)
committerMichael Meskes <[email protected]>
Tue, 27 Feb 2007 13:27:13 +0000 (13:27 +0000)
src/interfaces/ecpg/ecpglib/execute.c

index d02823c0a0fe74c237f0f88626746e843d4b8ad7..2eab5dea44cd589a7a816c2a74d8a3c608ad05ac 100644 (file)
 static char *
 quote_postgres(char *arg, bool quote, int lineno)
 {
-       char       *res;
-       int                     i,
-                               ri = 0;
+       char    *res;
+       int     error;
+       size_t  length;
+       size_t  escaped_len;
+       size_t  buffer_len;
 
        /*
         * if quote is false we just need to store things in a descriptor they
@@ -50,29 +52,35 @@ quote_postgres(char *arg, bool quote, int lineno)
                return res = ECPGstrdup(arg, lineno);
        else
        {
-               res = (char *) ECPGalloc(2 * strlen(arg) + 3, lineno);
+               length = strlen(arg);
+               buffer_len = 2 * length + 1;
+               res = (char *) ECPGalloc(buffer_len + 3, lineno);
                if (!res)
                        return (res);
 
-               /*
-                * We don't know if the target database is using
-                * standard_conforming_strings, so we always use E'' strings.
-                */
-               if (strchr(arg, '\\') != NULL)
-                       res[ri++] = ESCAPE_STRING_SYNTAX;
-
-               res[ri++] = '\'';
-
-               for (i = 0; arg[i]; i++, ri++)
+               error = 0;
+               escaped_len = PQescapeString(res+1, arg, buffer_len);
+               if (error)
                {
-                       if (SQL_STR_DOUBLE(arg[i], true))
-                               res[ri++] = arg[i];
-                       res[ri] = arg[i];
+                       ECPGfree(res);
+                       return NULL;
+               }
+               if (length == escaped_len)
+               {
+                       res[0] = res[escaped_len+1] = '\'';
+                       res[escaped_len+2] = '\0';
+               }
+               else
+               {
+                       /* 
+                        * We don't know if the target database is using
+                        * standard_conforming_strings, so we always use E'' strings.
+                        */
+                       memmove(res+2, res+1, escaped_len);
+                       res[0] = ESCAPE_STRING_SYNTAX;
+                       res[1] = res[escaped_len+2] = '\'';
+                       res[escaped_len+3] = '\0';
                }
-
-               res[ri++] = '\'';
-               res[ri] = '\0';
-
                ECPGfree(arg);
                return res;
        }