diff options
| author | Vibhav Pant | 2023-06-06 19:30:27 +0530 |
|---|---|---|
| committer | Vibhav Pant | 2023-06-06 19:30:27 +0530 |
| commit | 49ffcbf86a32a8a217538d4df3736fe069ccf35d (patch) | |
| tree | a5f16157cc20fb19a844473a6fbd2b434f4c8260 /src/sqlite.c | |
| parent | af569fa3d90a717983b743eb97adbf869c6d1736 (diff) | |
| parent | 7ca1d782f5910d0c3978c6798a45c6854ec668c7 (diff) | |
| download | emacs-49ffcbf86a32a8a217538d4df3736fe069ccf35d.tar.gz emacs-49ffcbf86a32a8a217538d4df3736fe069ccf35d.zip | |
Merge branch 'master' into scratch/comp-static-data
Diffstat (limited to 'src/sqlite.c')
| -rw-r--r-- | src/sqlite.c | 82 |
1 files changed, 65 insertions, 17 deletions
diff --git a/src/sqlite.c b/src/sqlite.c index 0361514766a..fd528f2b0d5 100644 --- a/src/sqlite.c +++ b/src/sqlite.c | |||
| @@ -23,6 +23,8 @@ YOSHIDA <syohex@gmail.com>, which can be found at: | |||
| 23 | https://github.com/syohex/emacs-sqlite3 */ | 23 | https://github.com/syohex/emacs-sqlite3 */ |
| 24 | 24 | ||
| 25 | #include <config.h> | 25 | #include <config.h> |
| 26 | |||
| 27 | #include <c-strcase.h> | ||
| 26 | #include "lisp.h" | 28 | #include "lisp.h" |
| 27 | #include "coding.h" | 29 | #include "coding.h" |
| 28 | 30 | ||
| @@ -30,6 +32,17 @@ YOSHIDA <syohex@gmail.com>, which can be found at: | |||
| 30 | 32 | ||
| 31 | #include <sqlite3.h> | 33 | #include <sqlite3.h> |
| 32 | 34 | ||
| 35 | /* Support for loading SQLite extensions requires the ability to | ||
| 36 | enable and disable loading of extensions (by default this is | ||
| 37 | disabled, and we want to keep it that way). The required macro is | ||
| 38 | available since SQLite 3.13. */ | ||
| 39 | # if defined HAVE_SQLITE3_LOAD_EXTENSION && \ | ||
| 40 | defined SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION | ||
| 41 | # define HAVE_LOAD_EXTENSION 1 | ||
| 42 | # else | ||
| 43 | # define HAVE_LOAD_EXTENSION 0 | ||
| 44 | # endif | ||
| 45 | |||
| 33 | #ifdef WINDOWSNT | 46 | #ifdef WINDOWSNT |
| 34 | 47 | ||
| 35 | # include <windows.h> | 48 | # include <windows.h> |
| @@ -75,11 +88,14 @@ DEF_DLL_FN (SQLITE_API int, sqlite3_exec, | |||
| 75 | DEF_DLL_FN (SQLITE_API int, sqlite3_prepare_v2, | 88 | DEF_DLL_FN (SQLITE_API int, sqlite3_prepare_v2, |
| 76 | (sqlite3*, const char*, int, sqlite3_stmt**, const char**)); | 89 | (sqlite3*, const char*, int, sqlite3_stmt**, const char**)); |
| 77 | 90 | ||
| 78 | # ifdef HAVE_SQLITE3_LOAD_EXTENSION | 91 | # if HAVE_LOAD_EXTENSION |
| 79 | DEF_DLL_FN (SQLITE_API int, sqlite3_load_extension, | 92 | DEF_DLL_FN (SQLITE_API int, sqlite3_load_extension, |
| 80 | (sqlite3*, const char*, const char*, char**)); | 93 | (sqlite3*, const char*, const char*, char**)); |
| 81 | # undef sqlite3_load_extension | 94 | # undef sqlite3_load_extension |
| 82 | # define sqlite3_load_extension fn_sqlite3_load_extension | 95 | # define sqlite3_load_extension fn_sqlite3_load_extension |
| 96 | DEF_DLL_FN (SQLITE_API int, sqlite3_db_config, (sqlite3*, int, ...)); | ||
| 97 | # undef sqlite3_db_config | ||
| 98 | # define sqlite3_db_config fn_sqlite3_db_config | ||
| 83 | # endif | 99 | # endif |
| 84 | 100 | ||
| 85 | # undef sqlite3_finalize | 101 | # undef sqlite3_finalize |
| @@ -170,8 +186,9 @@ load_dll_functions (HMODULE library) | |||
| 170 | LOAD_DLL_FN (library, sqlite3_column_text); | 186 | LOAD_DLL_FN (library, sqlite3_column_text); |
| 171 | LOAD_DLL_FN (library, sqlite3_column_name); | 187 | LOAD_DLL_FN (library, sqlite3_column_name); |
| 172 | LOAD_DLL_FN (library, sqlite3_exec); | 188 | LOAD_DLL_FN (library, sqlite3_exec); |
| 173 | # ifdef HAVE_SQLITE3_LOAD_EXTENSION | 189 | # if HAVE_LOAD_EXTENSION |
| 174 | LOAD_DLL_FN (library, sqlite3_load_extension); | 190 | LOAD_DLL_FN (library, sqlite3_load_extension); |
| 191 | LOAD_DLL_FN (library, sqlite3_db_config); | ||
| 175 | # endif | 192 | # endif |
| 176 | LOAD_DLL_FN (library, sqlite3_prepare_v2); | 193 | LOAD_DLL_FN (library, sqlite3_prepare_v2); |
| 177 | return true; | 194 | return true; |
| @@ -669,7 +686,7 @@ DEFUN ("sqlite-pragma", Fsqlite_pragma, Ssqlite_pragma, 2, 2, 0, | |||
| 669 | SSDATA (concat2 (build_string ("PRAGMA "), pragma))); | 686 | SSDATA (concat2 (build_string ("PRAGMA "), pragma))); |
| 670 | } | 687 | } |
| 671 | 688 | ||
| 672 | #ifdef HAVE_SQLITE3_LOAD_EXTENSION | 689 | #if HAVE_LOAD_EXTENSION |
| 673 | DEFUN ("sqlite-load-extension", Fsqlite_load_extension, | 690 | DEFUN ("sqlite-load-extension", Fsqlite_load_extension, |
| 674 | Ssqlite_load_extension, 2, 2, 0, | 691 | Ssqlite_load_extension, 2, 2, 0, |
| 675 | doc: /* Load an SQlite MODULE into DB. | 692 | doc: /* Load an SQlite MODULE into DB. |
| @@ -684,9 +701,28 @@ Only modules on Emacs' list of allowed modules can be loaded. */) | |||
| 684 | CHECK_STRING (module); | 701 | CHECK_STRING (module); |
| 685 | 702 | ||
| 686 | /* Add names of useful and free modules here. */ | 703 | /* Add names of useful and free modules here. */ |
| 687 | const char *allowlist[3] = { "pcre", "csvtable", NULL }; | 704 | const char *allowlist[] = { |
| 705 | "base64", | ||
| 706 | "cksumvfs", | ||
| 707 | "compress", | ||
| 708 | "csv", | ||
| 709 | "csvtable", | ||
| 710 | "fts3", | ||
| 711 | "icu", | ||
| 712 | "pcre", | ||
| 713 | "percentile", | ||
| 714 | "regexp", | ||
| 715 | "rot13", | ||
| 716 | "rtree", | ||
| 717 | "sha1", | ||
| 718 | "uuid", | ||
| 719 | "vfslog", | ||
| 720 | "zipfile", | ||
| 721 | NULL | ||
| 722 | }; | ||
| 688 | char *name = SSDATA (Ffile_name_nondirectory (module)); | 723 | char *name = SSDATA (Ffile_name_nondirectory (module)); |
| 689 | /* Possibly skip past a common prefix. */ | 724 | /* Possibly skip past a common prefix (libsqlite3_mod_ is used by |
| 725 | Debian, see https://packages.debian.org/source/sid/sqliteodbc). */ | ||
| 690 | const char *prefix = "libsqlite3_mod_"; | 726 | const char *prefix = "libsqlite3_mod_"; |
| 691 | if (!strncmp (name, prefix, strlen (prefix))) | 727 | if (!strncmp (name, prefix, strlen (prefix))) |
| 692 | name += strlen (prefix); | 728 | name += strlen (prefix); |
| @@ -694,10 +730,12 @@ Only modules on Emacs' list of allowed modules can be loaded. */) | |||
| 694 | bool do_allow = false; | 730 | bool do_allow = false; |
| 695 | for (const char **allow = allowlist; *allow; allow++) | 731 | for (const char **allow = allowlist; *allow; allow++) |
| 696 | { | 732 | { |
| 697 | if (strlen (*allow) < strlen (name) | 733 | ptrdiff_t allow_len = strlen (*allow); |
| 698 | && !strncmp (*allow, name, strlen (*allow)) | 734 | if (allow_len < strlen (name) |
| 699 | && (!strcmp (name + strlen (*allow), ".so") | 735 | && !strncmp (*allow, name, allow_len) |
| 700 | || !strcmp (name + strlen (*allow), ".DLL"))) | 736 | && (!strcmp (name + allow_len, ".so") |
| 737 | ||!strcmp (name + allow_len, ".dylib") | ||
| 738 | || !strcasecmp (name + allow_len, ".dll"))) | ||
| 701 | { | 739 | { |
| 702 | do_allow = true; | 740 | do_allow = true; |
| 703 | break; | 741 | break; |
| @@ -707,15 +745,25 @@ Only modules on Emacs' list of allowed modules can be loaded. */) | |||
| 707 | if (!do_allow) | 745 | if (!do_allow) |
| 708 | xsignal1 (Qsqlite_error, build_string ("Module name not on allowlist")); | 746 | xsignal1 (Qsqlite_error, build_string ("Module name not on allowlist")); |
| 709 | 747 | ||
| 710 | int result = sqlite3_load_extension | 748 | /* Expand all Lisp data explicitly, so as to avoid signaling an |
| 711 | (XSQLITE (db)->db, | 749 | error while extension loading is enabled -- we don't want to |
| 712 | SSDATA (ENCODE_FILE (Fexpand_file_name (module, Qnil))), | 750 | "leak" this outside this function. */ |
| 713 | NULL, NULL); | 751 | sqlite3 *sdb = XSQLITE (db)->db; |
| 714 | if (result == SQLITE_OK) | 752 | char *ext_fn = SSDATA (ENCODE_FILE (Fexpand_file_name (module, Qnil))); |
| 715 | return Qt; | 753 | /* Temporarily enable loading extensions via the C API. */ |
| 754 | int result = sqlite3_db_config (sdb, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, | ||
| 755 | NULL); | ||
| 756 | if (result == SQLITE_OK) | ||
| 757 | { | ||
| 758 | result = sqlite3_load_extension (sdb, ext_fn, NULL, NULL); | ||
| 759 | /* Disable loading extensions via C API. */ | ||
| 760 | sqlite3_db_config (sdb, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 0, NULL); | ||
| 761 | if (result == SQLITE_OK) | ||
| 762 | return Qt; | ||
| 763 | } | ||
| 716 | return Qnil; | 764 | return Qnil; |
| 717 | } | 765 | } |
| 718 | #endif /* HAVE_SQLITE3_LOAD_EXTENSION */ | 766 | #endif /* HAVE_LOAD_EXTENSION */ |
| 719 | 767 | ||
| 720 | DEFUN ("sqlite-next", Fsqlite_next, Ssqlite_next, 1, 1, 0, | 768 | DEFUN ("sqlite-next", Fsqlite_next, Ssqlite_next, 1, 1, 0, |
| 721 | doc: /* Return the next result set from SET. | 769 | doc: /* Return the next result set from SET. |
| @@ -825,7 +873,7 @@ syms_of_sqlite (void) | |||
| 825 | defsubr (&Ssqlite_commit); | 873 | defsubr (&Ssqlite_commit); |
| 826 | defsubr (&Ssqlite_rollback); | 874 | defsubr (&Ssqlite_rollback); |
| 827 | defsubr (&Ssqlite_pragma); | 875 | defsubr (&Ssqlite_pragma); |
| 828 | #ifdef HAVE_SQLITE3_LOAD_EXTENSION | 876 | #if HAVE_LOAD_EXTENSION |
| 829 | defsubr (&Ssqlite_load_extension); | 877 | defsubr (&Ssqlite_load_extension); |
| 830 | #endif | 878 | #endif |
| 831 | defsubr (&Ssqlite_next); | 879 | defsubr (&Ssqlite_next); |