MonetDB: Dec2023 - When printing USR1 info, if a needed lock is ...

2024-08-02 Thread Sjoerd Mullender via checkin-list
Changeset: a4e56273cf4c for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/a4e56273cf4c
Modified Files:
cmake/monetdb-defines.cmake
gdk/gdk_bbp.c
gdk/gdk_logger.c
gdk/gdk_system.c
gdk/gdk_system.h
monetdb_config.h.in
sql/backends/monet5/sql_scenario.c
sql/storage/store.c
Branch: Dec2023
Log Message:

When printing USR1 info, if a needed lock is held, wait a second and try again.
We try using a lock function with timeout, but if not available, we just
sleep for a second.


diffs (199 lines):

diff --git a/cmake/monetdb-defines.cmake b/cmake/monetdb-defines.cmake
--- a/cmake/monetdb-defines.cmake
+++ b/cmake/monetdb-defines.cmake
@@ -124,6 +124,7 @@ function(monetdb_configure_defines)
   cmake_push_check_state()
 set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
 check_function_exists("pthread_kill" HAVE_PTHREAD_KILL)
+check_function_exists("pthread_mutex_timedlock" 
HAVE_PTHREAD_MUTEX_TIMEDLOCK)
 check_function_exists("pthread_setname_np" HAVE_PTHREAD_SETNAME_NP)
 check_function_exists("pthread_sigmask" HAVE_PTHREAD_SIGMASK)
   cmake_pop_check_state()
diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -4936,6 +4936,15 @@ BBPtmlock(void)
BBPtmlockFinish();
 }
 
+static bool
+BBPtrytmlock(int ms)
+{
+   if (!MT_lock_trytime(&GDKtmLock, ms))
+   return false;
+   BBPtmlockFinish();
+   return true;
+}
+
 void
 BBPtmunlock(void)
 {
@@ -4959,7 +4968,10 @@ BBPprintinfo(void)
} bats[2][2][2][2][2] = {0};
int nbats = 0;
 
-   BBPtmlock();
+   if (!BBPtrytmlock(1000)) {
+   printf("BBP is currently locked, so no BAT information\n");
+   return;
+   }
bat sz = (bat) ATOMIC_GET(&BBPsize);
for (bat i = 1; i < sz; i++) {
MT_lock_set(&GDKswapLock(i));
diff --git a/gdk/gdk_logger.c b/gdk/gdk_logger.c
--- a/gdk/gdk_logger.c
+++ b/gdk/gdk_logger.c
@@ -1113,6 +1113,7 @@ log_create_types_file(logger *lg, const 
 
 #define rotation_lock(lg)  MT_lock_set(&(lg)->rotation_lock)
 #define rotation_unlock(lg)MT_lock_unset(&(lg)->rotation_lock)
+#define rotation_trylock(lg, ms) MT_lock_trytime(&(lg)->rotation_lock, ms)
 
 static gdk_return
 log_open_output(logger *lg)
@@ -3540,8 +3541,11 @@ log_tstart(logger *lg, bool flushnow, ul
 void
 log_printinfo(logger *lg)
 {
+   if (!rotation_trylock(lg, 1000)) {
+   printf("Logger is currently locked, so no logger 
information\n");
+   return;
+   }
printf("logger %s:\n", lg->fn);
-   rotation_lock(lg);
printf("current log file "ULLFMT", last handled log file "ULLFMT"\n",
   lg->id, lg->saved_id);
printf("current transaction id %d, saved transaction id %d\n",
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -239,6 +239,7 @@ struct mtthread mainthread = {
 static pthread_mutex_t posthread_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_key_t threadkey;
 #define thread_lock()  pthread_mutex_lock(&posthread_lock)
+#define thread_lock_try()  (pthread_mutex_trylock(&posthread_lock) == 0)
 #define thread_unlock()pthread_mutex_unlock(&posthread_lock)
 #define thread_self()  pthread_getspecific(threadkey)
 #define thread_setself(self)   pthread_setspecific(threadkey, self)
@@ -246,6 +247,7 @@ static pthread_key_t threadkey;
 static CRITICAL_SECTION winthread_cs;
 static DWORD threadkey = TLS_OUT_OF_INDEXES;
 #define thread_lock()  EnterCriticalSection(&winthread_cs)
+#define thread_lock_try()  (TryEnterCriticalSection(&winthread_cs) != 0)
 #define thread_unlock()LeaveCriticalSection(&winthread_cs)
 #define thread_self()  TlsGetValue(threadkey)
 #define thread_setself(self)   TlsSetValue(threadkey, self)
@@ -285,7 +287,23 @@ void
 dump_threads(void)
 {
char buf[1024];
-   thread_lock();
+#if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) && defined(HAVE_CLOCK_GETTIME)
+   struct timespec ts;
+   clock_gettime(CLOCK_REALTIME, &ts);
+   ts.tv_sec++;/* give it a second */
+   if (pthread_mutex_timedlock(&posthread_lock, &ts) != 0) {
+   printf("Threads are currently locked, so no thread 
information\n");
+   return;
+   }
+#else
+   if (!thread_lock_try()) {
+   MT_sleep_ms(1000);
+   if (!thread_lock_try()) {
+   printf("Threads are currently locked, so no thread 
information\n");
+   return;
+   }
+   }
+#endif
if (!GDK_TRACER_TEST(M_DEBUG, THRD))
printf("Threads:\n");
for (struct mtthread *t = mtthreads; t; t = t->next) {
diff --git a/gdk/gdk_system.h b/gdk/gdk_system.h
--- a/gdk/gdk_system.h
+++ b/gdk/gdk_system.h
@@ -497,6 +497,24 @@ typedef struct MT_Lock {
 
 #define MT_lock_try(l) 

MonetDB: Dec2023 - Use locks with timeouts on more locks during ...

2024-08-02 Thread Sjoerd Mullender via checkin-list
Changeset: e1bf9453989a for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/e1bf9453989a
Modified Files:
gdk/gdk_bbp.c
Branch: Dec2023
Log Message:

Use locks with timeouts on more locks during SIGUSR1 printing.


diffs (40 lines):

diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -4967,6 +4967,7 @@ BBPprintinfo(void)
int nr;
} bats[2][2][2][2][2] = {0};
int nbats = 0;
+   int nskip = 0;
 
if (!BBPtrytmlock(1000)) {
printf("BBP is currently locked, so no BAT information\n");
@@ -4974,13 +4975,19 @@ BBPprintinfo(void)
}
bat sz = (bat) ATOMIC_GET(&BBPsize);
for (bat i = 1; i < sz; i++) {
-   MT_lock_set(&GDKswapLock(i));
+   if (!MT_lock_trytime(&GDKswapLock(i), 1000)) {
+   nskip++;
+   continue;
+   }
int r;
if ((r = BBP_refs(i)) > 0 || BBP_lrefs(i) > 0) {
BAT *b = BBP_desc(i);
+   if (b != NULL && !MT_lock_trytime(&b->theaplock, 1000)) 
{
+   nskip++;
+   b = NULL;
+   }
if (b != NULL) {
nbats++;
-   MT_lock_set(&b->theaplock);
ATOMIC_BASE_TYPE status = BBP_status(i);
struct counters *bt = &bats[r > 
0][BATdirty(b)][(status & BBPPERSISTENT) != 0][(status & BBPLOADED) != 
0][(status & BBPHOT) != 0];
bt->nr++;
@@ -5067,4 +5074,6 @@ BBPprintinfo(void)
 
printf("%d bats total, %d in use, %"PRIu32" free bats in common shared 
list\n",
   sz - 1, nbats, nfree);
+   if (nskip > 0)
+   printf("%d bat slots unaccounted for because of locking\n", 
nskip);
 }
___
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org


MonetDB: Aug2024 - Avoid check before lock and recheck after.

2024-08-02 Thread Sjoerd Mullender via checkin-list
Changeset: dcc59ef76fb9 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/dcc59ef76fb9
Modified Files:
gdk/gdk_hash.c
gdk/gdk_imprints.c
gdk/gdk_orderidx.c
gdk/gdk_strimps.c
Branch: Aug2024
Log Message:

Avoid check before lock and recheck after.
Ordering of statements isn't guaranteed for this to work.


diffs (269 lines):

diff --git a/gdk/gdk_hash.c b/gdk/gdk_hash.c
--- a/gdk/gdk_hash.c
+++ b/gdk/gdk_hash.c
@@ -1358,7 +1358,7 @@ HASHlist(Hash *h, BUN i)
 void
 HASHdestroy(BAT *b)
 {
-   if (b && b->thash) {
+   if (b) {
Hash *hs;
MT_rwlock_wrlock(&b->thashlock);
hs = b->thash;
@@ -1371,7 +1371,7 @@ HASHdestroy(BAT *b)
 void
 HASHfree(BAT *b)
 {
-   if (b && b->thash) {
+   if (b) {
Hash *h;
MT_rwlock_wrlock(&b->thashlock);
if ((h = b->thash) != NULL && h != (Hash *) 1) {
diff --git a/gdk/gdk_imprints.c b/gdk/gdk_imprints.c
--- a/gdk/gdk_imprints.c
+++ b/gdk/gdk_imprints.c
@@ -314,75 +314,73 @@ BATcheckimprints(BAT *b)
}
}
 
+   MT_lock_set(&b->batIdxLock);
if (b->timprints == (Imprints *) 1) {
-   MT_lock_set(&b->batIdxLock);
-   if (b->timprints == (Imprints *) 1) {
-   Imprints *imprints;
-   const char *nme = BBP_physical(b->batCacheid);
+   Imprints *imprints;
+   const char *nme = BBP_physical(b->batCacheid);
+
+   assert(!GDKinmemory(bi.h->farmid));
+   b->timprints = NULL;
+   if ((imprints = GDKzalloc(sizeof(Imprints))) != NULL &&
+   (imprints->imprints.farmid = BBPselectfarm(b->batRole, 
bi.type, imprintsheap)) >= 0) {
+   int fd;
 
-   assert(!GDKinmemory(bi.h->farmid));
-   b->timprints = NULL;
-   if ((imprints = GDKzalloc(sizeof(Imprints))) != NULL &&
-   (imprints->imprints.farmid = 
BBPselectfarm(b->batRole, bi.type, imprintsheap)) >= 0) {
-   int fd;
-
-   strconcat_len(imprints->imprints.filename,
- 
sizeof(imprints->imprints.filename),
- nme, ".timprints", NULL);
-   imprints->imprints.storage = 
imprints->imprints.newstorage = STORE_INVALID;
-   /* check whether a persisted imprints index
-* can be found */
-   if ((fd = 
GDKfdlocate(imprints->imprints.farmid, nme, "rb", "timprints")) >= 0) {
-   size_t hdata[4];
-   struct stat st;
-   size_t pages;
+   strconcat_len(imprints->imprints.filename,
+ sizeof(imprints->imprints.filename),
+ nme, ".timprints", NULL);
+   imprints->imprints.storage = 
imprints->imprints.newstorage = STORE_INVALID;
+   /* check whether a persisted imprints index
+* can be found */
+   if ((fd = GDKfdlocate(imprints->imprints.farmid, nme, 
"rb", "timprints")) >= 0) {
+   size_t hdata[4];
+   struct stat st;
+   size_t pages;
 
-   pages = (((size_t) bi.count * bi.width) 
+ IMPS_PAGE - 1) / IMPS_PAGE;
-   if (read(fd, hdata, sizeof(hdata)) == 
sizeof(hdata) &&
-   hdata[0] & ((size_t) 1 << 16) &&
-   ((hdata[0] & 0xFF00) >> 8) == 
IMPRINTS_VERSION &&
-   hdata[3] == (size_t) bi.count &&
-   fstat(fd, &st) == 0 &&
-   st.st_size >= (off_t) 
(imprints->imprints.size =
-  
imprints->imprints.free =
-  64 * 
bi.width +
-  64 * 3 * 
SIZEOF_BUN +
-  pages * 
((bte) hdata[0] / 8) +
-  hdata[2] * 
sizeof(cchdc_t) +
-  
sizeof(uint64_t) /* padding for alignment */
-  + 4 * 
SIZEOF_SIZE_T) &&
-   HEAPload(&imprints->imprints, nme, 
"timprints", false) == GDK_SUCCEED) {
-

MonetDB: Aug2024 - Merge with Dec2023 branch.

2024-08-02 Thread Sjoerd Mullender via checkin-list
Changeset: 4eccf63aa8d6 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/4eccf63aa8d6
Modified Files:
cmake/monetdb-defines.cmake
gdk/gdk_bbp.c
gdk/gdk_logger.c
gdk/gdk_system.c
gdk/gdk_system.h
monetdb_config.h.in
sql/backends/monet5/sql_scenario.c
sql/storage/store.c
Branch: Aug2024
Log Message:

Merge with Dec2023 branch.


diffs (250 lines):

diff --git a/cmake/monetdb-defines.cmake b/cmake/monetdb-defines.cmake
--- a/cmake/monetdb-defines.cmake
+++ b/cmake/monetdb-defines.cmake
@@ -111,6 +111,7 @@ function(monetdb_configure_defines)
   cmake_push_check_state()
 set(CMAKE_REQUIRED_LIBRARIES "${CMAKE_THREAD_LIBS_INIT}")
 check_function_exists("pthread_kill" HAVE_PTHREAD_KILL)
+check_function_exists("pthread_mutex_timedlock" 
HAVE_PTHREAD_MUTEX_TIMEDLOCK)
 check_function_exists("pthread_setname_np" HAVE_PTHREAD_SETNAME_NP)
 check_function_exists("pthread_sigmask" HAVE_PTHREAD_SIGMASK)
   cmake_pop_check_state()
diff --git a/gdk/gdk_bbp.c b/gdk/gdk_bbp.c
--- a/gdk/gdk_bbp.c
+++ b/gdk/gdk_bbp.c
@@ -4863,6 +4863,15 @@ BBPtmlock(void)
BBPtmlockFinish();
 }
 
+static bool
+BBPtrytmlock(int ms)
+{
+   if (!MT_lock_trytime(&GDKtmLock, ms))
+   return false;
+   BBPtmlockFinish();
+   return true;
+}
+
 void
 BBPtmunlock(void)
 {
@@ -4885,28 +4894,40 @@ BBPprintinfo(void)
int nr;
} bats[2][2][2][2][2] = {0};
int nbats = 0;
-
-   BBPtmlock();
+   int nskip = 0;
+
+   if (!BBPtrytmlock(1000)) {
+   printf("BBP is currently locked, so no BAT information\n");
+   return;
+   }
bat sz = (bat) ATOMIC_GET(&BBPsize);
for (bat i = 1; i < sz; i++) {
-   MT_lock_set(&GDKswapLock(i));
+   if (!MT_lock_trytime(&GDKswapLock(i), 1000)) {
+   nskip++;
+   continue;
+   }
int r;
if ((r = BBP_refs(i)) > 0 || BBP_lrefs(i) > 0) {
BAT *b = BBP_desc(i);
-   nbats++;
-   MT_lock_set(&b->theaplock);
-   ATOMIC_BASE_TYPE status = BBP_status(i);
-   struct counters *bt = &bats[r > 0][BATdirty(b)][(status 
& BBPPERSISTENT) != 0][(status & BBPLOADED) != 0][(status & BBPHOT) != 0];
-   bt->nr++;
-   if (b->theap && b->batCacheid == b->theap->parentid) {
-   bt->sz += HEAPmemsize(b->theap);
-   bt->vmsz += HEAPvmsize(b->theap);
+   if (!MT_lock_trytime(&b->theaplock, 1000)) {
+   nskip++;
+   b = NULL;
}
-   if (b->tvheap && b->batCacheid == b->tvheap->parentid) {
-   bt->sz += HEAPmemsize(b->tvheap);
-   bt->vmsz += HEAPvmsize(b->tvheap);
+   if (b != NULL) {
+   nbats++;
+   ATOMIC_BASE_TYPE status = BBP_status(i);
+   struct counters *bt = &bats[r > 
0][BATdirty(b)][(status & BBPPERSISTENT) != 0][(status & BBPLOADED) != 
0][(status & BBPHOT) != 0];
+   bt->nr++;
+   if (b->theap && b->batCacheid == 
b->theap->parentid) {
+   bt->sz += HEAPmemsize(b->theap);
+   bt->vmsz += HEAPvmsize(b->theap);
+   }
+   if (b->tvheap && b->batCacheid == 
b->tvheap->parentid) {
+   bt->sz += HEAPmemsize(b->tvheap);
+   bt->vmsz += HEAPvmsize(b->tvheap);
+   }
+   MT_lock_unset(&b->theaplock);
}
-   MT_lock_unset(&b->theaplock);
}
MT_lock_unset(&GDKswapLock(i));
}
@@ -4980,4 +5001,6 @@ BBPprintinfo(void)
 
printf("%d bats total, %d in use, %"PRIu32" free bats in common shared 
list\n",
   sz - 1, nbats, nfree);
+   if (nskip > 0)
+   printf("%d bat slots unaccounted for because of locking\n", 
nskip);
 }
diff --git a/gdk/gdk_logger.c b/gdk/gdk_logger.c
--- a/gdk/gdk_logger.c
+++ b/gdk/gdk_logger.c
@@ -1115,6 +1115,7 @@ log_create_types_file(logger *lg, const 
 
 #define rotation_lock(lg)  MT_lock_set(&(lg)->rotation_lock)
 #define rotation_unlock(lg)MT_lock_unset(&(lg)->rotation_lock)
+#define rotation_trylock(lg, ms) MT_lock_trytime(&(lg)->rotation_lock, ms)
 
 static gdk_return
 log_open_output(logger *lg)
@@ -3502,8 +3503,11 @@ log_tstart(logger *lg, bool flushnow, ul
 void
 log_printinfo(logger *lg)
 {
+   if (!rotation_trylock(lg, 1

MonetDB: Aug2024 - make sure all ignore case values are equal.

2024-08-02 Thread Niels Nes via checkin-list
Changeset: c4be5a7db233 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/c4be5a7db233
Modified Files:
monetdb5/modules/atoms/str.c
Branch: Aug2024
Log Message:

make sure all ignore case values are equal.


diffs (33 lines):

diff --git a/monetdb5/modules/atoms/str.c b/monetdb5/modules/atoms/str.c
--- a/monetdb5/modules/atoms/str.c
+++ b/monetdb5/modules/atoms/str.c
@@ -2875,19 +2875,22 @@ ignorecase(const bat *ic_id, bool *icase
if ((c = BATdescriptor(*ic_id)) == NULL)
throw(MAL, fname, SQLSTATE(HY002) RUNTIME_OBJECT_MISSING);
 
-   if (BATcount(c) != 1) {
-   BUN cnt = BATcount(c);
+   BUN cnt = BATcount(c);
+   if (cnt < 1) {
BBPreclaim(c);
-   if (cnt == 0)
-   throw(MAL, fname, SQLSTATE(42000) "Missing ignore case 
value\n");
-   else
-   throw(MAL, fname, SQLSTATE(42000) "Multiple ignore case 
values passed, only one expected\n");
+   throw(MAL, fname, SQLSTATE(42000) "Missing ignore case 
value\n");
}
 
BATiter bi = bat_iterator(c);
*icase = *(bit *) BUNtloc(bi, 0);
+   for(BUN i = 1; i

MonetDB: Aug2024 - Produce correct error message when there is a...

2024-08-02 Thread Sjoerd Mullender via checkin-list
Changeset: 34a11d2bc555 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/34a11d2bc555
Modified Files:
monetdb5/mal/mal_interpreter.c
Branch: Aug2024
Log Message:

Produce correct error message when there is a timeout.


diffs (25 lines):

diff --git a/monetdb5/mal/mal_interpreter.c b/monetdb5/mal/mal_interpreter.c
--- a/monetdb5/mal/mal_interpreter.c
+++ b/monetdb5/mal/mal_interpreter.c
@@ -593,9 +593,18 @@ runMALsequence(Client cntxt, MalBlkPtr m
if (cntxt->fdin && TIMEOUT_TEST(&cntxt->qryctx)) {
if (cntxt->qryctx.endtime != QRY_INTERRUPT && 
cntxt->qryctx.endtime != QRY_TIMEOUT)
cntxt->mode = FINISHCLIENT;
-   stkpc = stoppc;
-   ret = createException(MAL, "mal.interpreter", 
"%s",
- 
TIMEOUT_MESSAGE(&cntxt->qryctx));
+   switch (cntxt->qryctx.endtime) {
+   case QRY_TIMEOUT:
+   ret = createException(MAL, 
"mal.interpreter", SQLSTATE(HYT00) RUNTIME_QRY_TIMEOUT);
+   break;
+   case QRY_INTERRUPT:
+   ret = createException(MAL, 
"mal.interpreter", SQLSTATE(HYT00) RUNTIME_QRY_INTERRUPT);
+   break;
+   default:
+   ret = createException(MAL, 
"mal.interpreter", SQLSTATE(HYT00) "Client disconnected");
+   cntxt->mode = FINISHCLIENT;
+   break;
+   }
break;
}
lastcheck = runtimeProfile.ticks;
___
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org


MonetDB: default - Merge with Aug2024 branch.

2024-08-02 Thread Sjoerd Mullender via checkin-list
Changeset: 80b87492af25 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/80b87492af25
Modified Files:
clients/Tests/MAL-signatures-hge.test
clients/Tests/MAL-signatures.test
clients/Tests/exports.stable.out
monetdb5/modules/kernel/algebra.c
sql/backends/monet5/sql.c
sql/storage/store.c
testing/Mtest.py.in
Branch: default
Log Message:

Merge with Aug2024 branch.


diffs (truncated from 2861 to 300 lines):

diff --git a/clients/Tests/MAL-signatures-hge.test 
b/clients/Tests/MAL-signatures-hge.test
--- a/clients/Tests/MAL-signatures-hge.test
+++ b/clients/Tests/MAL-signatures-hge.test
@@ -46584,11 +46584,6 @@ pattern io.printf(X_0:streams, X_1:str, 
 IOprintfStream;
 Select default format 
 io
-setmallocsuccesscount
-command io.setmallocsuccesscount(X_0:lng):void
-IOsetmallocsuccesscount;
-Set number of mallocs that are allowed to succeed.
-io
 stdin
 pattern io.stdin():bstream
 io_stdin;
diff --git a/clients/Tests/MAL-signatures.test 
b/clients/Tests/MAL-signatures.test
--- a/clients/Tests/MAL-signatures.test
+++ b/clients/Tests/MAL-signatures.test
@@ -35074,11 +35074,6 @@ pattern io.printf(X_0:streams, X_1:str, 
 IOprintfStream;
 Select default format 
 io
-setmallocsuccesscount
-command io.setmallocsuccesscount(X_0:lng):void
-IOsetmallocsuccesscount;
-Set number of mallocs that are allowed to succeed.
-io
 stdin
 pattern io.stdin():bstream
 io_stdin;
diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -319,7 +319,6 @@ void GDKreset(int status);
 void GDKsetbuf(char *);
 void GDKsetdebug(unsigned debug);
 gdk_return GDKsetenv(const char *name, const char *value);
-void GDKsetmallocsuccesscount(lng count);
 stream *GDKstdin;
 stream *GDKstdout;
 ssize_t GDKstrFromStr(unsigned char *restrict dst, const unsigned char 
*restrict src, ssize_t len, char quote);
diff --git a/clients/mapiclient/dump.c b/clients/mapiclient/dump.c
--- a/clients/mapiclient/dump.c
+++ b/clients/mapiclient/dump.c
@@ -1286,6 +1286,8 @@ describe_table(Mapi mid, const char *sch
 
s = sescape(schema);
t = sescape(tname);
+   if (s == NULL || t == NULL)
+   goto bailout;
maxquerylen = 5120 + strlen(t) + strlen(s);
query = malloc(maxquerylen);
if (query == NULL)
@@ -2249,7 +2251,11 @@ dump_table(Mapi mid, const char *schema,
goto doreturn;
}
for (int64_t i = 0; i < rows; i++) {
-   mapi_fetch_row(hdl);
+   if (mapi_fetch_row(hdl) == 0) {
+   mapi_close_handle(hdl);
+   fprintf(stderr, "unexepcted error\n");
+   goto doreturn;
+   }
tables[i].schema = strdup(mapi_fetch_field(hdl, 
0));
tables[i].table = strdup(mapi_fetch_field(hdl, 
1));
if (tables[i].schema == NULL || tables[i].table 
== NULL) {
@@ -3365,7 +3371,6 @@ dump_database(Mapi mid, stream *sqlf, co
mnstr_printf(sqlf, " %sCYCLE;\n", strcmp(cycle, "true") 
== 0 ? "" : "NO ");
if (mnstr_errnr(sqlf) != MNSTR_NO__ERROR) {
mapi_close_handle(hdl);
-   hdl = NULL;
goto bailout2;
}
}
diff --git a/clients/mapiclient/mclient.c b/clients/mapiclient/mclient.c
--- a/clients/mapiclient/mclient.c
+++ b/clients/mapiclient/mclient.c
@@ -3223,7 +3223,8 @@ putfile(void *data, const char *filename
close_stream(priv->f);
priv->f = NULL;
if (fname) {
-   MT_remove(fname);
+   if (MT_remove(fname) < 0)
+   perror(fname);
free(fname);
}
if (filename == NULL)
diff --git a/clients/mapilib/connect.c b/clients/mapilib/connect.c
--- a/clients/mapilib/connect.c
+++ b/clients/mapilib/connect.c
@@ -430,7 +430,7 @@ send_all_clientinfo(Mapi mid)
 
if (hostname[0])
reallocprintf(&buf, &pos, &cap, "ClientHostname=%s\n", 
hostname);
-   if (application_name[0])
+   if (application_name && application_name[0])
reallocprintf(&buf, &pos, &cap, "ApplicationName=%s\n", 
application_name);
reallocprintf(&buf, &pos, &cap, "ClientLibrary=");
if (mid->clientprefix)
@@ -466,7 +466,7 @@ mapi_handshake(Mapi mid)
 
/* consume server challenge */
len = mnstr_read_block(mid->from, buf, 1, sizeof(buf));
-   check_stream(mid, mid->from, "Connection terminated while starting 
handshake", (mid->blk.eos = true, mid->error));
+ 

MonetDB: Aug2024 - Fixes inspired by coverity.

2024-08-02 Thread Sjoerd Mullender via checkin-list
Changeset: 1cff4b89ae28 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/1cff4b89ae28
Modified Files:
gdk/gdk_hash.c
gdk/gdk_logger.c
gdk/gdk_system.c
gdk/gdk_unique.c
geom/monetdb5/geod.c
geom/monetdb5/geom.c
Branch: Aug2024
Log Message:

Fixes inspired by coverity.


diffs (282 lines):

diff --git a/gdk/gdk_hash.c b/gdk/gdk_hash.c
--- a/gdk/gdk_hash.c
+++ b/gdk/gdk_hash.c
@@ -1015,6 +1015,9 @@ BAThash(BAT *b)
if (BATcheckhash(b)) {
return GDK_SUCCEED;
}
+#ifdef __COVERITY__
+   MT_rwlock_wrlock(&b->thashlock);
+#else
for (;;) {
/* If multiple threads simultaneously try to build a
 * hash on a bat, e.g. in order to perform a join, it
@@ -1037,6 +1040,7 @@ BAThash(BAT *b)
MT_rwlock_rdunlock(&b->thashlock);
}
}
+#endif
/* we have the write lock */
if (b->thash == NULL) {
struct canditer ci;
diff --git a/gdk/gdk_logger.c b/gdk/gdk_logger.c
--- a/gdk/gdk_logger.c
+++ b/gdk/gdk_logger.c
@@ -1260,7 +1260,7 @@ log_read_transaction(logger *lg, uint32_
case LOG_UPDATE:
case LOG_CREATE:
case LOG_DESTROY:
-   if (updated && BAThash(lg->catalog_id) == GDK_SUCCEED) {
+   if (tr != NULL && updated && BAThash(lg->catalog_id) == 
GDK_SUCCEED) {
BATiter cni = bat_iterator(lg->catalog_id);
BUN p;
BUN posnew = BUN_NONE;
@@ -3229,8 +3229,8 @@ log_tdone(logger *lg, logged_range *rang
 gdk_return
 log_tflush(logger *lg, ulng file_id, ulng commit_ts)
 {
+   rotation_lock(lg);
if (lg->flushnow) {
-   rotation_lock(lg);
logged_range *p = lg->current;
assert(lg->flush_ranges == lg->current);
assert(ATOMIC_GET(&lg->current->flushed_ts) == 
ATOMIC_GET(&lg->current->last_ts));
@@ -3247,10 +3247,11 @@ log_tflush(logger *lg, ulng file_id, uln
return log_commit(lg, p, NULL, 0);
}
 
-   if (LOG_DISABLED(lg))
+   if (LOG_DISABLED(lg)) {
+   rotation_unlock(lg);
return GDK_SUCCEED;
-
-   rotation_lock(lg);
+   }
+
logged_range *frange = do_flush_range_cleanup(lg);
 
while (frange->next && frange->id < file_id) {
@@ -3454,7 +3455,7 @@ log_tstart(logger *lg, bool flushnow, ul
return GDK_SUCCEED;
}
/* I am now the exclusive flusher */
-   if (ATOMIC_GET(&lg->nr_flushers)) {
+   while (ATOMIC_GET(&lg->nr_flushers)) {
/* I am waiting until all existing flushers are done */
MT_cond_wait(&lg->excl_flush_cv, &lg->rotation_lock);
}
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -193,7 +193,7 @@ struct thread_funcs {
void *data;
 };
 
-static struct mtthread {
+struct mtthread {
struct mtthread *next;
void (*func) (void *);  /* function to be called */
void *data; /* and its data */
@@ -228,13 +228,15 @@ static struct mtthread {
uintptr_t sp;
char *errbuf;
struct freebats freebats;
-} *mtthreads = NULL;
-struct mtthread mainthread = {
+};
+static struct mtthread mainthread = {
.threadname = "main thread",
.exited = ATOMIC_VAR_INIT(0),
.refs = 1,
.tid = 1,
 };
+static struct mtthread *mtthreads = &mainthread;
+
 #ifdef HAVE_PTHREAD_H
 static pthread_mutex_t posthread_lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_key_t threadkey;
@@ -404,8 +406,6 @@ MT_thread_init(void)
}
InitializeCriticalSection(&winthread_cs);
 #endif
-   mainthread.next = NULL;
-   mtthreads = &mainthread;
thread_initialized = true;
return true;
 }
diff --git a/gdk/gdk_unique.c b/gdk/gdk_unique.c
--- a/gdk/gdk_unique.c
+++ b/gdk/gdk_unique.c
@@ -178,11 +178,8 @@ BATunique(BAT *b, BAT *s)
 (b->batRole == PERSISTENT && GDKinmemory(0))) &&
ci.ncand == bi.count &&
BAThash(b) == GDK_SUCCEED)) {
-   BUN lo = 0;
-
/* we already have a hash table on b, or b is
-* persistent and we could create a hash table, or b
-* is a view on a bat that already has a hash table */
+* persistent and we could create a hash table */
algomsg = "unique: existing hash";
MT_rwlock_rdlock(&b->thashlock);
hs = b->thash;
@@ -196,18 +193,18 @@ BATunique(BAT *b, BAT *s)
o = canditer_next(&ci);
p = o - hseq;
v = VALUE(p);
-   for (hb = HASHgetlink(hs, p + lo);
-