]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Strip unterminated "--" comments from the end of constraints added using ALTER TABLE...
authordan <Dan Kennedy>
Wed, 19 Nov 2025 11:31:59 +0000 (11:31 +0000)
committerdan <Dan Kennedy>
Wed, 19 Nov 2025 11:31:59 +0000 (11:31 +0000)
FossilOrigin-Name: c73ab47b0250c01f1ffa6e506793769744ebeb8ca7edbb78de9a08db2037d6de

manifest
manifest.uuid
src/alter.c
test/altercons2.test

index 6687e8b96fe3ca4acc068105918e79a122b773ed..3f7837e793074be1f9d73b652617d6489caf7571 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Clarification\sto\sthe\sheader\scomment\son\sdropConstraintFunc().\s\sNo\schanges\nto\scode.
-D 2025-11-19T11:28:48.761
+C Strip\sunterminated\s"--"\scomments\sfrom\sthe\send\sof\sconstraints\sadded\susing\sALTER\sTABLE\sADD\sCONSTRAINT\sor\sADD\sNOT\sNULL.
+D 2025-11-19T11:31:59.076
 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
@@ -666,7 +666,7 @@ F mptest/multiwrite01.test dab5c5f8f9534971efce679152c5146da265222d
 F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b
 F sqlite3.1 1b9c24374a85dfc7eb8fa7c4266ee0db4f9609cceecfc5481cd8307e5af04366
 F sqlite3.pc.in e6dee284fba59ef500092fdc1843df3be8433323a3733c91da96690a50a5b398
-F src/alter.c 912b1f084d184a48b6c7fbf7c081678126d3743d0236c0f7399364475d255c21
+F src/alter.c e7ee3350daae58a1f44822fbddae9c27190d1398142c4e0dd1077fd5bbb6bbde
 F src/analyze.c 03bcfc083fc0cccaa9ded93604e1d4244ea245c17285d463ef6a60425fcb247d
 F src/attach.c 9af61b63b10ee702b1594ecd24fb8cea0839cfdb6addee52fba26fa879f5db9d
 F src/auth.c 54ab9c6c5803b47c0d45b76ce27eff22a03b4b1f767c5945a3a4eb13aa4c78dc
@@ -837,7 +837,7 @@ F test/alterauth.test 63442ba61ceb0c1eeb63aac1f4f5cebfa509d352276059d27106ae256b
 F test/alterauth2.test 4b74fa8f184f4736497317feb587b65759eb87d87acfe3a8ef433d4d18bb002b
 F test/altercol.test 3661c432aacb42bc2198dd4611bbb9c3b09fc73251b59edda046109103b8ac00
 F test/altercons.test ea18def4a0f26b9066da56095c9c480df705df4d02e4ae151708fae76f7e3884
-F test/altercons2.test 23202f119ff158c0228cb24c6b2d35e923e812976b0a2a972401d2e40aac35b4
+F test/altercons2.test 3c1f58312817df43aeada3b1827fdc3ce3fc50c6f49a95ef62cf4cbbae8583a0
 F test/altercorrupt.test 2e1d705342cf9d7de884518ddbb053fd52d7e60d2b8869b7b63b2fda68435c12
 F test/alterdropcol.test a653a3945f964d26845ec0cd0a8e74189f46de3119a984c5bc45457da392612e
 F test/alterdropcol2.test 527fce683b200d620f560f666c44ae33e22728e990a10a48a543280dfd4b4d41
@@ -2177,8 +2177,8 @@ F tool/version-info.c 33d0390ef484b3b1cb685d59362be891ea162123cea181cb8e6d2cf6dd
 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7
 F tool/warnings.sh d924598cf2f55a4ecbc2aeb055c10bd5f48114793e7ba25f9585435da29e7e98
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P 383965b3ba701d3627e0bbd09ad04d0ce00d4721497d82e241dc7cffa745b1d9
-R 5ac491715339686ef4f784d6e85c19ed
-U drh
-Z aabf50dc517882f9aafdd99220ad9632
+P 4bd53fbb20d78de7bbff1154e7ee1321b93b0ede435e150ad4ee992a4108ad5a
+R e4fa4c468d8c8146033fef14ba9e155a
+U dan
+Z 282dcbcbadb63a9b3049c653389a681d
 # Remove this line to create a well-formed Fossil manifest.
index 2fa35d37e3680ee449def2422baaa9c94093786c..1dfa7c8eeed78965fd6370ffe08b725c17e7b97a 100644 (file)
@@ -1 +1 @@
-4bd53fbb20d78de7bbff1154e7ee1321b93b0ede435e150ad4ee992a4108ad5a
+c73ab47b0250c01f1ffa6e506793769744ebeb8ca7edbb78de9a08db2037d6de
index 2ed00f7cbec4fa2b7973b712fdc100c7a0637343..64c07162da9abd3e531f4823f79bdb8fbf516533 100644 (file)
@@ -2803,6 +2803,41 @@ static void failConstraintFunc(
   sqlite3_result_error_code(ctx, err);
 }
 
+/*
+** Buffer pCons, which is nCons bytes in size, contains the text of a 
+** NOT NULL or CHECK constraint that will be inserted into a CREATE TABLE
+** statement. If successful, this function returns the size of the buffer in
+** bytes not including any trailing whitespace or "--" style comments. Or,
+** if an OOM occurs, it returns 0 and sets db->mallocFailed to true.
+*/
+static int alterRtrimConstraint(
+  sqlite3 *db,                    /* used to record OOM error */
+  const char *pCons,              /* Buffer containing constraint */
+  int nCons                       /* Size of pCons in bytes */
+){
+  u8 *zTmp = (u8*)sqlite3_mprintf("%.*s", nCons, pCons);
+  int iOff = 0;
+  int iEnd = 0;
+
+  if( zTmp==0 ){
+    db->mallocFailed = 1;
+    return 0;
+  }
+
+  while( 1 ){
+    int t = 0;
+    int nToken = sqlite3GetToken(&zTmp[iOff], &t);
+    if( t==TK_ILLEGAL ) break;
+    if( t!=TK_SPACE && (t!=TK_COMMENT || zTmp[iOff]!='-') ){
+      iEnd = iOff+nToken;
+    }
+    iOff += nToken;
+  }
+
+  sqlite3_free(zTmp);
+  return iEnd;
+}
+
 /*
 ** Prepare a statement of the form:
 **
@@ -2832,8 +2867,7 @@ void sqlite3AlterSetNotNull(
 
   /* Find the length in bytes of the constraint definition */
   pCons = pFirst->z;
-  nCons = pParse->sLastToken.z - pCons;
-  while( sqlite3Isspace(pCons[nCons-1]) ) nCons--;
+  nCons = alterRtrimConstraint(pParse->db, pCons, pParse->sLastToken.z - pCons);
 
   /* Search for a constraint violation. Throw an exception if one is found. */
   sqlite3NestedParse(pParse,
@@ -2905,7 +2939,7 @@ void sqlite3AlterAddConstraint(
   Parse *pParse,                  /* Parse context */
   SrcList *pSrc,                  /* Table to add constraint to */
   Token *pFirst,                  /* First token of new constraint */
-  Token *pCons,                   /* Name of new constraint */
+  Token *pName,                   /* Name of new constraint */
   const char *pExpr,              /* Text of CHECK expression */
   int nExpr                       /* Size of pExpr in bytes */
 ){ 
@@ -2913,6 +2947,7 @@ void sqlite3AlterAddConstraint(
   int iDb = 0;
   const char *zDb = 0;
   int nCons;
+  const char *pCons = 0;
 
   /* Look up the table being altered. */
   pTab = alterFindTable(pParse, pSrc, &iDb, &zDb, 1);
@@ -2920,8 +2955,8 @@ void sqlite3AlterAddConstraint(
 
   /* If this new constraint has a name, check that it is not a duplicate of
   ** an existing constraint. It is an error if it is.  */
-  if( pCons ){
-    char *zName = sqlite3NameFromToken(pParse->db, pCons);
+  if( pName ){
+    char *zName = sqlite3NameFromToken(pParse->db, pName);
 
     sqlite3NestedParse(pParse,
         "SELECT sqlite_fail('constraint %q already exists', %d) "
@@ -2941,13 +2976,14 @@ void sqlite3AlterAddConstraint(
   );
 
   /* Edit the SQL for the named table. */
-  nCons = pParse->sLastToken.z - pFirst->z;
-  while( sqlite3Isspace(pFirst->z[nCons-1]) ) nCons--;
+  pCons = pFirst->z;
+  nCons = alterRtrimConstraint(pParse->db, pCons, pParse->sLastToken.z - pCons);
+
   sqlite3NestedParse(pParse,
       "UPDATE \"%w\"." LEGACY_SCHEMA_TABLE " SET "
       "sql = sqlite_add_constraint(sql, %.*Q, -1) "
       "WHERE type='table' AND tbl_name=%Q COLLATE nocase"
-      , zDb, nCons, pFirst->z, pTab->zName
+      , zDb, nCons, pCons, pTab->zName
   );
 
   /* Finally, reload the database schema. */
index a0e9ca6a806062a152600b4021156fe37986b243..a5bbaf6fc6bce82351bf73c2b11de9601c7b1d97 100644 (file)
@@ -206,5 +206,42 @@ do_execsql_test 10.4 {
   INSERT INTO abc VALUES(NULL);
 }
 
+#-------------------------------------------------------------------------
+reset_db
+do_execsql_test 11.0 {
+  CREATE TABLE t1(a, b, c);
+}
+
+do_execsql_test 11.1.1 {
+  ALTER TABLE t1 ADD CONSTRAINT c1 CHECK(a=b) --comment
+  ;
+}
+
+do_execsql_test 11.1.2 {ALTER TABLE t1 ADD CONSTRAINT c2 CHECK(a=b) --comment}
+
+do_execsql_test 11.1.3 {
+  SELECT sql FROM sqlite_schema;
+} {
+  {CREATE TABLE t1(a, b, c, CONSTRAINT c1 CHECK(a=b), CONSTRAINT c2 CHECK(a=b))}
+}
+
+do_execsql_test 11.2.1 {
+  CREATE TABLE t2(a, b);
+}
+do_execsql_test 11.2.2 {ALTER TABLE t2 ALTER b SET NOT NULL --new cons}
+do_execsql_test 11.2.3 {
+  SELECT sql FROM sqlite_schema WHERE name='t2';
+} {
+  {CREATE TABLE t2(a, b NOT NULL)}
+}
+do_execsql_test 11.2.3 {
+  ALTER TABLE t2 ALTER b DROP NOT NULL;
+  SELECT sql FROM sqlite_schema WHERE name='t2';
+} {
+  {CREATE TABLE t2(a, b)}
+}
+do_execsql_test 11.2.2 {ALTER TABLE t2 ALTER b SET NOT NULL --new cons
+;
+}
 finish_test