Fix detection of unfinished Unicode surrogate pair at end of string.
authorTom Lane
Wed, 21 Dec 2016 22:39:33 +0000 (17:39 -0500)
committerTom Lane
Wed, 21 Dec 2016 22:39:33 +0000 (17:39 -0500)
The U&'...' and U&"..." syntaxes silently discarded a surrogate pair
start (that is, a code between U+D800 and U+DBFF) if it occurred at
the very end of the string.  This seems like an obvious oversight,
since we throw an error for every other invalid combination of surrogate
characters, including the very same situation in E'...' syntax.

This has been wrong since the pair processing was added (in 9.0),
so back-patch to all supported branches.

Discussion: https://postgr.es/m/19113.1482337898@sss.pgh.pa.us

src/backend/parser/scan.l

index 5364942701c9ab068f3d594a41baf3a31dc3ee8f..85cf7a35cb14ced54db0dcf90f97b90c060914ea 100644 (file)
@@ -1395,7 +1395,15 @@ litbuf_udeescape(unsigned char escape, core_yyscan_t yyscanner)
        }
    }
 
+   /* unfinished surrogate pair? */
+   if (pair_first)
+   {
+       ADVANCE_YYLLOC(in - litbuf + 3);                /* 3 for U&" */
+       yyerror("invalid Unicode surrogate pair");
+   }
+
    *out = '\0';
+
    /*
     * We could skip pg_verifymbstr if we didn't process any non-7-bit-ASCII
     * codes; but it's probably not worth the trouble, since this isn't