Changeset: 1dcaa2b1e5cb for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=1dcaa2b1e5cb
Modified Files:
        monetdb5/modules/atoms/str.c
        sql/test/SQLancer/Tests/sqlancer01.sql
        sql/test/SQLancer/Tests/sqlancer01.stable.out
Branch: Jun2020
Log Message:

Fix for bug 6887, handle NULL and empty strings properly at replace(string) 
function


diffs (68 lines):

diff --git a/monetdb5/modules/atoms/str.c b/monetdb5/modules/atoms/str.c
--- a/monetdb5/modules/atoms/str.c
+++ b/monetdb5/modules/atoms/str.c
@@ -3989,23 +3989,26 @@ STRSubstitute(str *res, const str *arg1,
        const char *pfnd;
        char *fnd;
 
-       if (strNil(s)) {
+       if (strNil(s) || strNil(src) || strNil(dst)) {
                if ((*res = GDKstrdup(str_nil)) == NULL)
                        throw(MAL, "str.substitute", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
                return MAL_SUCCEED;
        }
+       if (!lsrc || !l) { /* s/src is an empty string, there's nothing to 
substitute */
+               if ((*res = GDKstrdup(s)) == NULL)
+                       throw(MAL, "str.substitute", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
+               return MAL_SUCCEED;
+       }
 
        n = l + ldst;
-       if (repeat && ldst > lsrc && lsrc) {
+       if (repeat && ldst > lsrc)
                n = (ldst * l) / lsrc;  /* max length */
-       }
+
        buf = *res = GDKmalloc(n);
        if (*res == NULL)
                throw(MAL, "str.substitute", SQLSTATE(HY013) MAL_MALLOC_FAIL);
 
        pfnd = s;
-       if (lsrc == 0)
-               lsrc = 1;                               /* make sure we make 
progress */
        do {
                fnd = strstr(pfnd, src);
                if (fnd == NULL)
diff --git a/sql/test/SQLancer/Tests/sqlancer01.sql 
b/sql/test/SQLancer/Tests/sqlancer01.sql
--- a/sql/test/SQLancer/Tests/sqlancer01.sql
+++ b/sql/test/SQLancer/Tests/sqlancer01.sql
@@ -35,6 +35,7 @@ ROLLBACK;
 
 -- Bug 6887
 SELECT "sys"."replace"(cast(cast(0.8009925043335998 as clob) as 
clob(169)),cast("sys"."replace"('!','','wtkg춑5,I}楘') as 
clob),"sys"."concat"("sys"."concat"('?dMHr펔2!FU4Rᔎ%',-1194732688),0.7566860950241294));
+SELECT "replace"('!','','wtkg춑5,I}楘'), "replace"('','!','wtkg춑5,I}楘'), 
"replace"('abc',NULL,'wtkg춑5,I}楘'), "replace"('abc','b',NULL);
 
 DROP TABLE tbl_ProductSales;
 DROP TABLE another_T;
diff --git a/sql/test/SQLancer/Tests/sqlancer01.stable.out 
b/sql/test/SQLancer/Tests/sqlancer01.stable.out
--- a/sql/test/SQLancer/Tests/sqlancer01.stable.out
+++ b/sql/test/SQLancer/Tests/sqlancer01.stable.out
@@ -113,6 +113,18 @@ stdout of test 'sqlancer01` in directory
 [ 4    ]
 #ALTER TABLE another_t ADD UNIQUE(col8, col1, col6, col3);
 #ROLLBACK;
+#SELECT "sys"."replace"(cast(cast(0.8009925043335998 as clob) as 
clob(169)),cast("sys"."replace"('!','','wtkg춑5,I}楘') as 
clob),"sys"."concat"("sys"."concat"('?dMHr펔2!FU4Rᔎ%',-1194732688),0.7566860950241294));
+% .%2 # table_name
+% %2 # name
+% varchar # type
+% 18 # length
+[ "0.8009925043335998" ]
+#SELECT "replace"('!','','wtkg춑5,I}楘'), "replace"('','!','wtkg춑5,I}楘'), 
"replace"('abc',NULL,'wtkg춑5,I}楘'), "replace"('abc','b',NULL);
+% .%2, .%3,    .%4,    .%5 # table_name
+% %2,  %3,     %4,     %5 # name
+% varchar,     varchar,        clob,   clob # type
+% 1,   0,      0,      0 # length
+[ "!", "",     NULL,   NULL    ]
 #DROP TABLE tbl_ProductSales;
 #DROP TABLE another_T;
 #DROP TABLE integers;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to