diff --git a/test/wasm_depth.c b/test/wasm_depth.c index 0f696b07..888f418c 100644 --- a/test/wasm_depth.c +++ b/test/wasm_depth.c @@ -5,82 +5,62 @@ #include #include -static bool check_depth(const char *name, const char *open, int depth, - bool nested, bool expect_error) +/* prefix, open repeated depth times, body, close repeated depth times, suffix */ +static char *nested_query(const char *prefix, const char *open, const char *body, + const char *close, const char *suffix, int depth) { - const char prefix[] = "SELECT "; - size_t open_len = strlen(open); - char *query = malloc(sizeof(prefix) + depth * (open_len + nested) + 1); + char *query = malloc(strlen(prefix) + depth * (strlen(open) + strlen(close)) + + strlen(body) + strlen(suffix) + 1); if (query == NULL) { - fprintf(stderr, "FAIL %s: could not allocate query\n", name); - return false; + fprintf(stderr, "FAIL: could not allocate query\n"); + exit(EXIT_FAILURE); } - char *p = query; - memcpy(p, prefix, sizeof(prefix) - 1); - p += sizeof(prefix) - 1; + char *p = stpcpy(query, prefix); for (int i = 0; i < depth; i++) - { - memcpy(p, open, open_len); - p += open_len; - } - *p++ = '1'; - if (nested) - { - memset(p, ')', depth); - p += depth; - } - *p = '\0'; + p = stpcpy(p, open); + p = stpcpy(p, body); + for (int i = 0; i < depth; i++) + p = stpcpy(p, close); + strcpy(p, suffix); + return query; +} - PgQueryParseResult result = pg_query_parse(query); - const char *error = result.error ? result.error->message : NULL; - bool ok = expect_error ? error && strcmp(error, "stack depth limit exceeded") == 0 - : result.error == NULL && result.parse_tree != NULL; +static bool check_result(const char *name, const PgQueryError *error, bool has_tree, + bool expect_error) +{ + const char *expected = expect_error ? "stack depth limit exceeded" : "a parse tree"; + const char *got = error ? error->message : has_tree ? "a parse tree" : "no parse tree"; + bool ok = strcmp(expected, got) == 0; if (ok) - printf("PASS %s: %s\n", name, expect_error ? error : "parsed successfully"); + printf("PASS %s: %s\n", name, got); else - fprintf(stderr, "FAIL %s: expected %s, got %s\n", name, - expect_error ? "stack depth limit exceeded" : "a parse tree", - error ? error : result.parse_tree ? "a parse tree" : "no parse tree"); + fprintf(stderr, "FAIL %s: expected %s, got %s\n", name, expected, got); + return ok; +} + +/* e.g. "SELECT " "f(" ... "1" ... ")" */ +static bool check_depth(const char *name, const char *open, const char *close, int depth, + bool expect_error) +{ + char *query = nested_query("SELECT ", open, "1", close, "", depth); + PgQueryParseResult result = pg_query_parse(query); + bool ok = check_result(name, result.error, result.parse_tree != NULL, expect_error); pg_query_free_parse_result(result); free(query); return ok; } -/* depth nested PL/pgSQL blocks, e.g. "IF true THEN " ... "NULL;" ... " END IF;" */ +/* e.g. "IF true THEN " ... "NULL;" ... " END IF;" in a function body */ static bool check_plpgsql_depth(const char *name, const char *open, const char *body, const char *close, int depth) { - const char prefix[] = "CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN "; - const char suffix[] = " END $$"; - size_t open_len = strlen(open), body_len = strlen(body), close_len = strlen(close); - char *query = malloc(sizeof(prefix) + depth * (open_len + close_len) + body_len + sizeof(suffix)); - if (query == NULL) - { - fprintf(stderr, "FAIL %s: could not allocate query\n", name); - return false; - } - - char *p = query; - memcpy(p, prefix, sizeof(prefix) - 1); - p += sizeof(prefix) - 1; - for (int i = 0; i < depth; i++, p += open_len) - memcpy(p, open, open_len); - memcpy(p, body, body_len); - p += body_len; - for (int i = 0; i < depth; i++, p += close_len) - memcpy(p, close, close_len); - memcpy(p, suffix, sizeof(suffix)); - + char *query = nested_query("CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN ", + open, body, close, " END $$", depth); PgQueryPlpgsqlParseResult result = pg_query_parse_plpgsql(query); - bool ok = result.error == NULL && result.plpgsql_funcs != NULL; - if (ok) - printf("PASS %s: parsed successfully\n", name); - else - fprintf(stderr, "FAIL %s: expected a parse tree, got %s\n", name, - result.error ? result.error->message : "no parse tree"); + bool ok = check_result(name, result.error, result.plpgsql_funcs != NULL, false); pg_query_free_plpgsql_parse_result(result); free(query); @@ -90,21 +70,7 @@ static bool check_plpgsql_depth(const char *name, const char *open, const char * /* depth chained terms through pg_query_normalize, e.g. "SELECT 1+1+1" */ static bool check_normalize_depth(const char *name, int depth) { - const char prefix[] = "SELECT 1"; - char *query = malloc(sizeof(prefix) + (depth - 1) * 2); - if (query == NULL) - { - fprintf(stderr, "FAIL %s: could not allocate query\n", name); - return false; - } - - char *p = query; - memcpy(p, prefix, sizeof(prefix) - 1); - p += sizeof(prefix) - 1; - for (int i = 1; i < depth; i++, p += 2) - memcpy(p, "+1", 2); - *p = '\0'; - + char *query = nested_query("SELECT 1", "+1", "", "", "", depth - 1); PgQueryNormalizeResult result = pg_query_normalize(query); int params = 0; if (result.normalized_query != NULL) @@ -130,28 +96,19 @@ static bool check_normalize_depth(const char *name, int depth) */ int main(int argc, char **argv) { - int status = EXIT_SUCCESS; + bool ok = true; if (argc > 1 && strcmp(argv[1], "overflow") == 0) - { - if (!check_depth("100000 chained terms", "1+", 99999, false, true)) - status = EXIT_FAILURE; - return status; - } + return check_depth("100000 chained terms", "1+", "", 99999, true) ? EXIT_SUCCESS : EXIT_FAILURE; + /* * First, while the host engine has not optimized anything yet: its native * stack then holds the fewest levels. */ - if (!check_normalize_depth("normalize 3000 chained terms", 3000)) - status = EXIT_FAILURE; - if (!check_depth("2000 chained terms", "1+", 1999, false, false)) - status = EXIT_FAILURE; - if (!check_depth("1000 nested subqueries", "(SELECT ", 1000, true, false)) - status = EXIT_FAILURE; - if (!check_depth("2000 nested function calls", "f(", 2000, true, false)) - status = EXIT_FAILURE; - if (!check_plpgsql_depth("1000 nested IF blocks", "IF true THEN ", "NULL;", " END IF;", 1000)) - status = EXIT_FAILURE; - if (!check_plpgsql_depth("3000 nested BEGIN blocks", "BEGIN ", "NULL;", " END;", 3000)) - status = EXIT_FAILURE; - return status; + ok &= check_normalize_depth("normalize 3000 chained terms", 3000); + ok &= check_depth("2000 chained terms", "1+", "", 1999, false); + ok &= check_depth("1000 nested subqueries", "(SELECT ", ")", 1000, false); + ok &= check_depth("2000 nested function calls", "f(", ")", 2000, false); + ok &= check_plpgsql_depth("1000 nested IF blocks", "IF true THEN ", "NULL;", " END IF;", 1000); + ok &= check_plpgsql_depth("3000 nested BEGIN blocks", "BEGIN ", "NULL;", " END;", 3000); + return ok ? EXIT_SUCCESS : EXIT_FAILURE; }