Changeset: 0b006567a159 for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/0b006567a159 Added Files: ctest/tools/monetdbe/example_sessions.c Modified Files: ctest/tools/monetdbe/CMakeLists.txt sql/backends/monet5/sql.c Branch: Aug2024 Log Message:
Fix #7589 segfault when accessing sys.sessions from monetdbe diffs (112 lines): diff --git a/ctest/tools/monetdbe/CMakeLists.txt b/ctest/tools/monetdbe/CMakeLists.txt --- a/ctest/tools/monetdbe/CMakeLists.txt +++ b/ctest/tools/monetdbe/CMakeLists.txt @@ -84,6 +84,13 @@ target_link_libraries(example_connection monetdbe) add_test(run_example_connections example_connections) +add_executable(example_sessions example_sessions.c) +target_link_libraries(example_sessions + PRIVATE + monetdb_config_header + monetdbe) +add_test(run_example_sessions example_sessions) + if(WITH_CMOCKA) add_executable(cmocka_test cmocka_test.c test_helper.c) target_include_directories(cmocka_test PRIVATE "${CMOCKA_INCLUDE_DIR}") diff --git a/ctest/tools/monetdbe/example_sessions.c b/ctest/tools/monetdbe/example_sessions.c new file mode 100644 --- /dev/null +++ b/ctest/tools/monetdbe/example_sessions.c @@ -0,0 +1,75 @@ +/* + * SPDX-License-Identifier: MPL-2.0 + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2024 MonetDB Foundation; + * Copyright August 2008 - 2023 MonetDB B.V.; + * Copyright 1997 - July 2008 CWI. + */ + +#include "monetdbe.h" +#include <stdlib.h> +#include <stdio.h> +#include <inttypes.h> + +#define error(msg) {fprintf(stderr, "Failure: %s\n", msg); return -1;} + +int +main(void) +{ + char* err = NULL; + monetdbe_database mdbe = NULL; + monetdbe_result* result = NULL; + + // second argument is a string for the db directory or NULL for in-memory mode + if (monetdbe_open(&mdbe, NULL, NULL)) + error("Failed to open database") + if ((err = monetdbe_query(mdbe, "SELECT * FROM sys.sessions", &result, NULL)) != NULL) + error(err) + + fprintf(stdout, "Query result with %zu cols and %"PRId64" rows\n", result->ncols, result->nrows); + for (int64_t r = 0; r < result->nrows; r++) { + for (size_t c = 0; c < result->ncols; c++) { + monetdbe_column* rcol; + if ((err = monetdbe_result_fetch(result, &rcol, c)) != NULL) + error(err) + switch (rcol->type) { + case monetdbe_int32_t: { + monetdbe_column_int32_t * col = (monetdbe_column_int32_t *) rcol; + if (col->data[r] == col->null_value) { + printf("NULL"); + } else { + printf("%d", col->data[r]); + } + break; + } + case monetdbe_str: { + monetdbe_column_str * col = (monetdbe_column_str *) rcol; + if (col->is_null(col->data+r)) { + printf("NULL"); + } else { + printf("%s", (char*) col->data[r]); + } + break; + } + default: { + printf("UNKNOWN"); + } + } + + if (c + 1 < result->ncols) { + printf(", "); + } + } + printf("\n"); + } + + if ((err = monetdbe_cleanup_result(mdbe, result)) != NULL) + error(err) + if (monetdbe_close(mdbe)) + error("Failed to close database") + return 0; +} diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c --- a/sql/backends/monet5/sql.c +++ b/sql/backends/monet5/sql.c @@ -3677,7 +3677,10 @@ sql_sessions_wrap(Client cntxt, MalBlkPt goto bailout; if (BUNappend(mlimit, &c->memorylimit, false) != GDK_SUCCEED) goto bailout; - if (BUNappend(language, getScenarioLanguage(c), false) != GDK_SUCCEED) + // If the scenario is NULL we assume we're in monetdbe/e which + // is always SQL. + s = c->scenario ? getScenarioLanguage(c) : "sql"; + if (BUNappend(language, s, false) != GDK_SUCCEED) goto bailout; s = c->peer ? c->peer : str_nil; if (BUNappend(peer, s, false) != GDK_SUCCEED) _______________________________________________ checkin-list mailing list -- checkin-list@monetdb.org To unsubscribe send an email to checkin-list-le...@monetdb.org