From d1b83991afc9698f76021b5cc9caf518ef0c600c Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Tue, 23 Apr 2024 19:13:53 +0300 Subject: [PATCH] Forbid zero values in sparsevec's binary input function (#528) The text input function simply left out any zero values, but the binary input function did not. That's problematic because you end up with an "unnormalized" sparse vector, which behaves in weird ways. At least sparsevec_cmp_internal() expects both inputs to not contain zeros. The binary send function never produces such zero values, but an external tool could. Or to test, you can use COPY TO (FORMAT BINARY), use a hex editor to edit one of the values to be zero, and copy it back with COPY FROM (FORMAT BINARY). --- src/sparsevec.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/sparsevec.c b/src/sparsevec.c index c38aff6..06edc35 100644 --- a/src/sparsevec.c +++ b/src/sparsevec.c @@ -527,6 +527,10 @@ sparsevec_recv(PG_FUNCTION_ARGS) { values[i] = pq_getmsgfloat4(buf); CheckElement(values[i]); + if (values[i] == 0) + ereport(ERROR, + (errcode(ERRCODE_DATA_EXCEPTION), + errmsg("binary representation of sparsevec cannot contain zero values"))); } PG_RETURN_POINTER(result);