After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 785983 - gom_command_builder_build_insert does not wrap table name in single-quotes
gom_command_builder_build_insert does not wrap table name in single-quotes
Status: RESOLVED FIXED
Product: gom
Classification: Other
Component: general
0.3.x
Other Linux
: Normal normal
: ---
Assigned To: Gom Maintainers
Gom Maintainers
Depends on:
Blocks:
 
 
Reported: 2017-08-08 04:55 UTC by Link Dupont
Modified: 2017-08-09 11:21 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
quote table name during insert (892 bytes, patch)
2017-08-08 04:55 UTC, Link Dupont
needs-work Details | Review
patch to modify test-gom-insert to use a failing table name (873 bytes, patch)
2017-08-09 04:27 UTC, Link Dupont
none Details | Review
tests: Add test for invalid table names (7.94 KB, patch)
2017-08-09 11:18 UTC, Bastien Nocera
committed Details | Review
gom: Add a private header to list reserved keywords (2.59 KB, patch)
2017-08-09 11:18 UTC, Bastien Nocera
committed Details | Review
gom: Verify whether a table name is valid before setting it (1.61 KB, patch)
2017-08-09 11:18 UTC, Bastien Nocera
committed Details | Review
tests: And check for assertion due to invalid table name (1.24 KB, patch)
2017-08-09 11:18 UTC, Bastien Nocera
committed Details | Review

Description Link Dupont 2017-08-08 04:55:09 UTC
Created attachment 357170 [details] [review]
quote table name during insert

STEPS TO REPRODUCE 

1. Create a GomResource subclass
2. In the class_init, call gom_resource_class_set_table, and set the table name to a SQLite keyword (such as insert, set, update)

EXPECTED RESULTS

The SQL INSERT command to succeed.

ACTUAL RESULTS

The SQL INSERT fails with sqlite3_prepare_v2 failing.

NOTES

This can be reproduced by using the test-gom-insert.c test, changing the table name of ItemResource to 'insert', and running test-gom-insert. This is fixed by the attached patch.
Comment 1 Bastien Nocera 2017-08-08 08:50:52 UTC
Review of attachment 357170 [details] [review]:

A separate patch to show the bug (the same one as in the description) would also be appreciated.

::: gom/gom-command-builder.c
@@ +736,2 @@
    str = g_string_new("INSERT INTO ");
+   g_string_append_printf(str, "'%s' (", klass->table);

The table name should have already been transformed into a unique not-clashing name at this point.

I'd make gom_resource_class_set_table() fail if the name is a reserved one.
Comment 2 Link Dupont 2017-08-09 04:27:57 UTC
Created attachment 357225 [details] [review]
patch to modify test-gom-insert to use a failing table name

Quick patch to cause the test-gom-insert test to fail with a reserved word table name ("insert").
Comment 3 Link Dupont 2017-08-09 04:38:58 UTC
(In reply to Bastien Nocera from comment #1)
> Review of attachment 357170 [details] [review] [review]:
> 
> A separate patch to show the bug (the same one as in the description) would
> also be appreciated.

Attached in comment #2.

> ::: gom/gom-command-builder.c
> @@ +736,2 @@
>     str = g_string_new("INSERT INTO ");
> +   g_string_append_printf(str, "'%s' (", klass->table);
> 
> The table name should have already been transformed into a unique
> not-clashing name at this point.

Unless I missed it, I briefly traced through gom_resource_class_set_table_name and gom_command_builder_build_insert. I didn't see anywhere that the table class variable was modified. It's run through snprintf() in _set_table_name, but it's not modified in any way.

   g_snprintf(resource_class->table,
              sizeof(resource_class->table),
              "%s", table);

> I'd make gom_resource_class_set_table() fail if the name is a reserved one.

I thought about this too. SQLite declares a list of reserved keywords[1]. They still recommend quoting English words even when unnecessary. I think perhaps this patch, plus an error check earlier in _set_table_name would be the best approach.

> SQLite adds new keywords from time to time when it takes on new features. So to prevent your code from being broken by future enhancements, you should normally quote any identifier that is an English language word, even if you do not have to.

1: http://sqlite.org/lang_keywords.html
Comment 4 Bastien Nocera 2017-08-09 11:18:36 UTC
Created attachment 357252 [details] [review]
tests: Add test for invalid table names
Comment 5 Bastien Nocera 2017-08-09 11:18:42 UTC
Created attachment 357253 [details] [review]
gom: Add a private header to list reserved keywords
Comment 6 Bastien Nocera 2017-08-09 11:18:48 UTC
Created attachment 357254 [details] [review]
gom: Verify whether a table name is valid before setting it
Comment 7 Bastien Nocera 2017-08-09 11:18:53 UTC
Created attachment 357255 [details] [review]
tests: And check for assertion due to invalid table name

The old test would simply crash when attempting to use a
resource with the invalid table name for the first time. With the
validity checks, we now assert much earlier. Adapt the test to succeed
if the check fails as expected.
Comment 8 Bastien Nocera 2017-08-09 11:20:49 UTC
The table name must now not be a reserved keyword, or the program will assert()
(as it's a programmer error).

Thanks for the report!

Attachment 357252 [details] pushed as 4ce0cb6 - tests: Add test for invalid table names
Attachment 357253 [details] pushed as 1df4aa6 - gom: Add a private header to list reserved keywords
Attachment 357254 [details] pushed as 8e17777 - gom: Verify whether a table name is valid before setting it
Attachment 357255 [details] pushed as 7cb346a - tests: And check for assertion due to invalid table name