pgvector.git / summary / log / commit / refs
commit 0ef0467a0fb2da0401296e3f8fc6b3f1f9a4f659
Author: Andrew Kane <andrew@ankane.org>
Date: Wed May 31 20:54:19 2023 +0000
Fixed infinite values with vector addition and subtraction
CHANGELOG.md | 1 +
src/vector.c | 8 ++++++++
test/expected/functions.out | 4 ++++
test/sql/functions.sql | 2 ++
4 files changed, 15 insertions(+)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5df63325..1f286deb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,6 @@
## 0.4.3 (unreleased)
+- Fixed infinite values with vector addition and subtraction
- Fixed compilation error when `float8` is pass by reference
- Fixed compilation error on PowerPC
- Fixed segmentation fault with index creation on i386
diff --git a/src/vector.c b/src/vector.c
index 13ff5147..392d98a7 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -656,6 +656,10 @@ vector_add(PG_FUNCTION_ARGS)
for (int i = 0, imax = a->dim; i < imax; i++)
rx[i] = ax[i] + bx[i];
+ for (int i = 0, imax = a->dim; i < imax; i++)
+ if (isinf(rx[i]))
+ float_overflow_error();
+
PG_RETURN_POINTER(result);
}
@@ -682,6 +686,10 @@ vector_sub(PG_FUNCTION_ARGS)
for (int i = 0, imax = a->dim; i < imax; i++)
rx[i] = ax[i] - bx[i];
+ for (int i = 0, imax = a->dim; i < imax; i++)
+ if (isinf(rx[i]))
+ float_overflow_error();
+
PG_RETURN_POINTER(result);
}
diff --git a/test/expected/functions.out b/test/expected/functions.out
index bbcea4dc..0272282f 100644
--- a/test/expected/functions.out
+++ b/test/expected/functions.out
@@ -4,12 +4,16 @@ SELECT '[1,2,3]'::vector + '[4,5,6]';
[5,7,9]
(1 row)
+SELECT '[3e38]'::vector + '[3e38]';
+ERROR: value out of range: overflow
SELECT '[1,2,3]'::vector - '[4,5,6]';
?column?
------------
[-3,-3,-3]
(1 row)
+SELECT '[-3e38]'::vector - '[3e38]';
+ERROR: value out of range: overflow
SELECT vector_dims('[1,2,3]');
vector_dims
-------------
diff --git a/test/sql/functions.sql b/test/sql/functions.sql
index 91f0570e..e4d33172 100644
--- a/test/sql/functions.sql
+++ b/test/sql/functions.sql
@@ -1,5 +1,7 @@
SELECT '[1,2,3]'::vector + '[4,5,6]';
+SELECT '[3e38]'::vector + '[3e38]';
SELECT '[1,2,3]'::vector - '[4,5,6]';
+SELECT '[-3e38]'::vector - '[3e38]';
SELECT vector_dims('[1,2,3]');
[parent: dee2c4feb1bc]