Skip to content

Commit

Permalink
protect against invalid ipix values
Browse files Browse the repository at this point in the history
add tests
add maximum possible ipix value into .h file
  • Loading branch information
segasai committed Feb 11, 2021
1 parent 16a4889 commit 3918ade
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ test: gen_data
psql q3c_test -c 'ANALYZE test_pm1'
psql q3c_test -c 'ANALYZE test_small'
mkdir -p results
cat sql/ang2ipix.sql | psql q3c_test > results/ang2ipix.out
cat sql/ang2ipix.sql | psql q3c_test > results/ang2ipix.out 2>&1
diff results/ang2ipix.out expected/ang2ipix.expected
cat sql/cone.sql | psql q3c_test > results/cone.out
cat sql/cone.sql | psql q3c_test > results/cone.out 2>&1
diff results/cone.out expected/cone.expected
cat sql/cone_join_rev.sql | psql q3c_test > results/cone.out
diff results/cone.out expected/cone.expected
Expand All @@ -99,7 +99,7 @@ test: gen_data
diff results/poly1.out expected/poly.expected
cat sql/version.sql | psql q3c_test > results/version.out
diff results/version.out expected/version.expected
cat sql/area.sql | psql q3c_test > results/area.out
cat sql/area.sql | psql q3c_test > results/area.out 2>&1
diff results/area.out expected/area.expected
dropdb q3c_test
createdb q3c_test
Expand Down
8 changes: 8 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ typedef int64 q3c_ipix_t;
#endif /* Q3C_IPIX_FMT */


#ifndef Q3C_MAX_IPIX
#define Q3C_MAX_IPIX ( (((q3c_ipix_t)1<<60)-1) + ((q3c_ipix_t)1<<60) + ((q3c_ipix_t)1<<62))
/* This is maximum allowed ipix value
101 + 1111111111111....111
60 ones
*/
#endif /* Q3C_MAX_IPIX */


/* If You have not specified the Q3C_LONG_DOUBLE macro then we will use simple
double functions */
Expand Down
6 changes: 6 additions & 0 deletions expected/ang2ipix.expected
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,9 @@
1000000
(1 row)

q3c_ipix2ang
------------------------
{315,35.2643897079092}
(1 row)

ERROR: Invalid ipix value
4 changes: 4 additions & 0 deletions expected/area.expected
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
t
(1 row)

ERROR: Invalid depth. It should be less than 31.
ERROR: Invalid depth. It should be greater than 0.
ERROR: Invalid ipix value
ERROR: Invalid ipix value
12 changes: 12 additions & 0 deletions q3c.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,11 @@ Datum pgq3c_ipix2ang(PG_FUNCTION_ARGS)
char typalign;
ArrayType *result;
ipix = PG_GETARG_INT64(0);
if ((ipix < 0) || (ipix > Q3C_MAX_IPIX))
{
elog(ERROR, "Invalid ipix value");

}
q3c_ipix2ang(&hprm, ipix, &ra, &dec);

data = ( Datum *) palloc(sizeof(Datum) * 2);
Expand Down Expand Up @@ -310,6 +314,14 @@ Datum pgq3c_pixarea(PG_FUNCTION_ARGS)
{
elog(ERROR, "Invalid depth. It should be less than 31.");
}
if (ipix < 0)
{
elog(ERROR, "Invalid ipix value");
}
if (ipix > Q3C_MAX_IPIX)
{
elog(ERROR, "Invalid ipix value");
}

res = q3c_pixarea(&hprm, ipix, depth);

Expand Down
2 changes: 2 additions & 0 deletions sql/ang2ipix.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ select q3c_dist_pm(0,0,3600000,0,0,0,90,0,90)<1e-9;
select q3c_dist_pm(0,0,0,3600000,0,0,0,90,90)<1e-9;
select sum((q3c_ang2ipix(((q3c_ipix2ang(q3c_ang2ipix(ra,dec)))[1]),(q3c_ipix2ang(q3c_ang2ipix(ra,dec)))[2])=q3c_ang2ipix(ra,dec))::int) from test;
select sum((q3c_ang2ipix(ra::real,dec::real) = q3c_ang2ipix( (ra::real)::double precision, (dec::real)::double precision))::int) from test;
select q3c_ipix2ang(0);
select q3c_ipix2ang(-1);
5 changes: 5 additions & 0 deletions sql/area.sql
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
select 6*q3c_pixarea(0,30)/pi()/4. between 0.99999 and 1.00001;
select q3c_pixarea(10,31);
select q3c_pixarea(10,0);
select q3c_pixarea(-1,4);
select q3c_pixarea(6917529027641081856,4);

0 comments on commit 3918ade

Please sign in to comment.