From d425b5059e7f6843259e6c486674e33bedac824e Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sun, 2 May 2021 18:16:07 +0200
Subject: [PATCH] CONFIG: Don't fail if sqlite3_hard_heap_limit64 is not
 present

---
 configure                 | 58 +++++++++++++++++++++++++++++++++++++--
 configure.ac              | 13 +++++++--
 inc/lektor/lktconfig.h.in |  3 ++
 src/database/open.c       |  9 ++++++
 4 files changed, 79 insertions(+), 4 deletions(-)

diff --git a/configure b/configure
index b52abded..3ac0c916 100755
--- a/configure
+++ b/configure
@@ -18721,8 +18721,6 @@ else
   LKT_SANITIZER=no
 fi
 
-# address => -fsanitizer=address,leak
-# thread  => -fsanitizer=thread
 if test "x$LKT_SANITIZER" == "xyes" ; then
     { $as_echo "$as_me:${as_lineno-$LINENO}: Build with sanitizers enabled" >&5
 $as_echo "$as_me: Build with sanitizers enabled" >&6;}
@@ -18900,6 +18898,62 @@ if test $? -ne 0 ; then
     as_fn_error $? "Faield to get sqlite3 with pkg-config, is dev lib for sqlite3 installed?" "$LINENO" 5
 fi
 
+#############################
+# Search for some functions #
+#############################
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for sqlite3_hard_heap_limit64 in -lsqlite3" >&5
+$as_echo_n "checking for sqlite3_hard_heap_limit64 in -lsqlite3... " >&6; }
+if ${ac_cv_lib_sqlite3_sqlite3_hard_heap_limit64+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  ac_check_lib_save_LIBS=$LIBS
+LIBS="-lsqlite3  $LIBS"
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char sqlite3_hard_heap_limit64 ();
+int
+main ()
+{
+return sqlite3_hard_heap_limit64 ();
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  ac_cv_lib_sqlite3_sqlite3_hard_heap_limit64=yes
+else
+  ac_cv_lib_sqlite3_sqlite3_hard_heap_limit64=no
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+LIBS=$ac_check_lib_save_LIBS
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_sqlite3_sqlite3_hard_heap_limit64" >&5
+$as_echo "$ac_cv_lib_sqlite3_sqlite3_hard_heap_limit64" >&6; }
+if test "x$ac_cv_lib_sqlite3_sqlite3_hard_heap_limit64" = xyes; then :
+
+    { $as_echo "$as_me:${as_lineno-$LINENO}: result: Lib sqlite3 contains sqlite3_hard_heap_limit64" >&5
+$as_echo "Lib sqlite3 contains sqlite3_hard_heap_limit64" >&6; }
+
+else
+
+    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: unable to find the sqlite3_hard_heap_limit64() function" >&5
+$as_echo "$as_me: WARNING: unable to find the sqlite3_hard_heap_limit64() function" >&2;}
+
+$as_echo "#define LKT_NO_SQLITE3_HARD_HEAP_LIMIT64_FUNCTION /**/" >>confdefs.h
+
+
+fi
+
+
 ################
 # CONF RESULTS #
 ################
diff --git a/configure.ac b/configure.ac
index 638134b4..ce79a2ec 100644
--- a/configure.ac
+++ b/configure.ac
@@ -295,8 +295,6 @@ AC_ARG_WITH([sanitizer],
                             [Build with sanitizer enabled, yes by default.])],
             [LKT_SANITIZER=$withval],
             [LKT_SANITIZER=no])
-# address => -fsanitizer=address,leak
-# thread  => -fsanitizer=thread
 if test "x$LKT_SANITIZER" == "xyes" ; then
     AC_MSG_NOTICE([Build with sanitizers enabled])
     SANITIZERS="undefined,shift-exponent,shift-base,vla-bound,nullsigned-integer-overflow,bounds-strict,float-cast-overflow,bool,enum,address"
@@ -432,6 +430,17 @@ if test $? -ne 0 ; then
     AC_MSG_ERROR([Faield to get sqlite3 with pkg-config, is dev lib for sqlite3 installed?])
 fi
 
+#############################
+# Search for some functions #
+#############################
+
+AC_CHECK_LIB([sqlite3], [sqlite3_hard_heap_limit64], [
+    AC_MSG_RESULT(Lib sqlite3 contains sqlite3_hard_heap_limit64)
+], [
+    AC_MSG_WARN([unable to find the sqlite3_hard_heap_limit64() function])
+    AC_DEFINE([LKT_NO_SQLITE3_HARD_HEAP_LIMIT64_FUNCTION], [], [No hard heap limit for sqlite3])
+])
+
 ################
 # CONF RESULTS #
 ################
diff --git a/inc/lektor/lktconfig.h.in b/inc/lektor/lktconfig.h.in
index 0dc82c4b..1fc8f420 100644
--- a/inc/lektor/lktconfig.h.in
+++ b/inc/lektor/lktconfig.h.in
@@ -96,6 +96,9 @@
 /* Path to the man executable */
 #undef LKT_MAN_BINARY
 
+/* No hard heap limit for sqlite3 */
+#undef LKT_NO_SQLITE3_HARD_HEAP_LIMIT64_FUNCTION
+
 /* Prefix Path for Lektor */
 #undef LKT_PREFIX_PATH
 
diff --git a/src/database/open.c b/src/database/open.c
index 3a0bde42..705dd1a9 100644
--- a/src/database/open.c
+++ b/src/database/open.c
@@ -16,6 +16,15 @@ static const char *__pragmas = "PRAGMA case_sensitive_like = false;\n"
 #define HEAP_LIMIT_SOFT 100 * 1024 * 1024
 #define HEAP_LIMIT_HARD 150 * 1024 * 1024
 
+#if defined(LKT_NO_SQLITE3_HARD_HEAP_LIMIT64_FUNCTION)
+sqlite3_int64
+sqlite3_hard_heap_limit64(sqlite3_int64 UNUSED limit)
+{
+    LOG_ERROR("DB", "No sqlite3_hard_heap_limit64 function on your system");
+    return 0;
+}
+#endif
+
 struct named_db {
     volatile sqlite3 *db;
     const char *name;
-- 
GitLab