Type parser: "(Int32)" is accepted, and the column type comes from uninitialized memory
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 82/100
Direzione di ricerca
Inizia da clickhouse/types/type_parser.h e clickhouse/types/type_parser.cpp, in particolare da ParseTypeName, TypeParser::Parse e dal percorso di convalida dell’AST descritto nel report. Verifica come vengono inizializzati i nodi radice e figli di TypeAst e come viene gestito un nodo che inizia con (. Il lavoro è completato quando i nomi di tipo non validi, come (Int32), vengono rifiutati in modo coerente, anche all’interno dei contenitori, senza leggere valori non inizializzati; l’issue non indica un file di test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
CreateColumnByType() accepts a type name that starts with (, for example (Int32), which the server rejects. On master, the type of the returned column depends on the memory contents before the call: in one Release build, the same call returned Array(Int32), Nullable(Int32), Tuple(Int32), UInt16, a ValidationError, or nullptr. The type-name cache keeps the first result, so all later calls with this name in the process return the same type. UBSan reports the read of an invalid enum value.
The read path of Select() (clickhouse/client.cpp:862) has the same behavior when a server or a proxy sends such a type name in a Data block (a correct server does not). With a test server that sends (Int32), Select() returned a block with an Array(Int32) column, or threw UnimplementedError: unsupported column type: (Int32) after another stack fill.
v2.6.2 always returns Array(Int32) for (Int32): the result started to depend on memory with #577 (not released yet). Inside a container, a node with no type name is always read as Array, on master and in v2.6.2: Array((Int32)) gives Array(Array(Int32)), and Map(String, (Int32)) gives Map(String, Array(Int32)).
Steps to reproduce
<fill> writes a 32-bit value over 64 KiB of stack before the call, to show the effect of the memory contents.
Reproduction program (C++, 33 lines)
// Lead check: CreateColumnByType on a type name that starts with "(".
// usage: root_ast <fill> <name> [<fill> <name> ...]
// <fill> = none, or a 32-bit value written over 64 KiB of stack before the call.
#include <clickhouse/columns/factory.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <typeinfo>
using namespace clickhouse;
__attribute__((noinline)) static void FillStack(uint32_t v) {
volatile uint32_t buf[16384];
for (size_t i = 0; i < 16384; ++i) buf[i] = v;
}
static std::string Try(const std::string& name) {
try {
auto col = CreateColumnByType(name);
return col ? "column " + col->Type()->GetName() : "nullptr";
} catch (const std::exception& e) {
return std::string("exception ") + typeid(e).name() + ": " + e.what();
}
}
int main(int argc, char** argv) {
for (int i = 1; i + 1 < argc; i += 2) {
std::string f = argv[i];
if (f != "none") FillStack(static_cast<uint32_t>(std::strtoul(f.c_str(), nullptr, 0)));
std::printf("fill=%-10s %-14s -> %s\n", f.c_str(), argv[i + 1], Try(argv[i + 1]).c_str());
}
return 0;
}
Linked with libclickhouse-cpp-lib.a of cmake -DCMAKE_BUILD_TYPE=Release. Each run of the loop is a new process:
$ for f in none 0 3 6 7 8 0x55555555; do ./root_ast $f '(Int32)'; done
fill=none (Int32) -> column Array(Int32)
fill=0 (Int32) -> column Array(Int32)
fill=3 (Int32) -> column Nullable(Int32)
fill=6 (Int32) -> column UInt16
fill=7 (Int32) -> column Tuple(Int32)
fill=8 (Int32) -> exception N10clickhouse15ValidationErrorE: content is not correct
fill=0x55555555 (Int32) -> nullptr
$ ./root_ast 3 '(Int32)' 7 '(Int32)' 0 '(Int32)'
fill=3 (Int32) -> column Nullable(Int32)
fill=7 (Int32) -> column Nullable(Int32)
fill=0 (Int32) -> column Nullable(Int32)
$ ./root_ast none 'Array((Int32))'
fill=none Array((Int32)) -> column Array(Array(Int32))
With -DCMAKE_CXX_FLAGS=-fsanitize=undefined and the fill 0x55555555:
clickhouse/types/type_parser.cpp:161:13: runtime error: load of value 1431655765, which is not a valid value for type 'Meta'
Expected behavior
CreateColumnByType() returns nullptr every time, as it does for other names that do not parse (Array(, )), and Select() throws UnimplementedError. The server rejects both names:
$ curl -s localhost:8123 --data-binary "SELECT CAST(1, '(Int32)')"
Code: 62. DB::Exception: Syntax error (data type): failed at position 1 ((): (Int32). Expected one of: data type, identifier: In scope SELECT CAST(1, '(Int32)'). (SYNTAX_ERROR) (version 26.11.7.10 (official build))
$ curl -s localhost:8123 --data-binary "SELECT CAST([[1]], 'Array((Int32))')"
Code: 62. DB::Exception: Syntax error (data type): failed at position 7 ((): (Int32)). ...
Root cause
ParseTypeName (clickhouse/types/type_parser.cpp:384) declares the root as TypeAst ast;, and TypeAst::meta and TypeAst::code (clickhouse/types/type_parser.h:29-30) have no default member initializers, so both are indeterminate. TypeParser::Parse sets them only when the node gets a name, number, or string token (type_parser.cpp:188-220), so for a name that starts with (, ValidateAST (:161) and CreateColumnFromAst read indeterminate values. Before #577, ast_cache[type_name] value-initialized the root. Child nodes are value-initialized (emplace_back(TypeAst()), :223), so their meta is 0 (Array). No check rejects ( on a node that has no type name.
The fix is a few lines in type_parser.h and TypeParser::Parse.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related
- #585 (open) creates the root with
std::make_shared<TypeAst>(). That makes the result a fixedArray(Int32)again, but the name is still accepted.
Environment
- clickhouse-cpp:
master6049285; v2.6.2 for the contrast - ClickHouse server: 26.11.7.10
- clang 18.1.3, Linux x86_64 (Ubuntu 24.04)
Classification: impact: none · area: Data types & formats (type names) · native: format
Found by automated analysis (Polyglot AI) while working on ClickHouse/clickhouse-rs#578. Verified against a live server.
- Lingua principale
- C
- Stelle
- 388
- Fork
- 220
- Merge medio
- 1g 25m
- PR unite (30g)
- 16
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di ClickHouse/clickhouse-cpp
-
bug data-types
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
ClickHouse/clickhouse-cpp#645 ·
I maintainer di solito rispondono entro 1 giorno
-
bug queries
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
ClickHouse/clickhouse-cpp#644 ·
I maintainer di solito rispondono entro 1 giorno
-
bug connectivity native-protocol
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
ClickHouse/clickhouse-cpp#638 ·
I maintainer di solito rispondono entro 1 giorno
-
Core SQL test harness: ParseSetAssignments does not skip comments inside SETForse già presa @puretechteam l’ha presa 12 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
ClickHouse/clickhouse-cpp#626 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Enum type parsing: duplicate ordinals (Enum8('a' = 1, 'b' = 1)) and duplicate labels (Enum8('a' = 1, 'a' = 2)) are silently accepted and lose a memberForse già presa @SBNREX l’ha presa 19 giorni fa. Apertabug data-types native-format
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
ClickHouse/clickhouse-cpp#607 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di ClickHouse/clickhouse-cpp
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
type:feature
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
google-coral/coralnpu#175 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno