aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2023-01-16 14:41:16 +0100
committerThomas White <taw@physics.org>2023-01-16 15:08:17 +0100
commitb3be317e1f147298da295ebcb423f6d26d4a830c (patch)
treedcafbb6403bd064c98e25973fc1287db079f6683
parent2d22cd6dca61d22ed654c0b6bbc4da740b32f35e (diff)
asdf: Avoid integer overflow with number of triplets
Co-authored-by: Alexandra Tolstikova <alexandra.tolstikova@desy.de> Fixes: https://gitlab.desy.de/thomas.white/crystfel/-/issues/76
-rw-r--r--libcrystfel/src/indexers/asdf.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/libcrystfel/src/indexers/asdf.c b/libcrystfel/src/indexers/asdf.c
index 39e8c5be..4473b3f2 100644
--- a/libcrystfel/src/indexers/asdf.c
+++ b/libcrystfel/src/indexers/asdf.c
@@ -887,9 +887,9 @@ long nCk(int n, int k) {
assert(k>=0 && k<4);
switch ( k ) {
case 0 : return 1;
- case 1 : return n;
- case 2 : return n*(n-1)/2;
- case 3 : return n*(n-1)*(n-2)/6;
+ case 1 : return (long)n;
+ case 2 : return (long)n*(n-1)/2;
+ case 3 : return (long)n*(n-1)*(n-2)/6;
}
return 0;
}
@@ -918,11 +918,13 @@ void get_triplet_by_index(int index, int n, int *triplet) {
static int **generate_triplets(int N_reflections, int N_triplets_max, int *N)
{
int i, n, ri;
- int N_triplets_tot = nCk(N_reflections, 3);
+ long int N_triplets_tot = nCk(N_reflections, 3);
- int N_triplets = N_triplets_tot;
- if ( N_triplets > N_triplets_max || N_reflections > 1000 ) {
+ int N_triplets;
+ if ( N_triplets_tot > N_triplets_max || N_reflections > 1000 ) {
N_triplets = N_triplets_max;
+ } else {
+ N_triplets = N_triplets_tot;
}
*N = N_triplets;