avoid integer overflow issues reported by CodeQL

This commit is contained in:
Axel Kohlmeyer
2021-07-03 17:28:23 -04:00
parent 4b42b51006
commit a61e79ad72
3 changed files with 48 additions and 46 deletions

View File

@ -431,7 +431,7 @@ void FixPolarizeBEMGMRES::gmres_solve(double *x, double *r)
// fill up h with zero // fill up h with zero
memset(h, 0, (mr + 1) * mr * sizeof(double)); memset(h, 0, (size_t)(mr + 1) * mr * sizeof(double));
// the inner loop k = 1..(n-1) // the inner loop k = 1..(n-1)
// build up the k-th Krylov space, // build up the k-th Krylov space,
@ -756,11 +756,11 @@ double FixPolarizeBEMGMRES::memory_usage()
bytes += atom->nmax * sizeof(double); // q_backup bytes += atom->nmax * sizeof(double); // q_backup
bytes += mr * sizeof(double); // c bytes += mr * sizeof(double); // c
bytes += (mr + 1) * sizeof(double); // g bytes += (mr + 1) * sizeof(double); // g
bytes += (mr + 1) * mr * sizeof(double); // h bytes += (double) (mr + 1) * mr * sizeof(double); // h
bytes += mat_dim * sizeof(double); // r bytes += mat_dim * sizeof(double); // r
bytes += mr * (mr + 1) * sizeof(double); // s bytes += (double) mr * (mr + 1) * sizeof(double); // s
bytes += mat_dim * sizeof(double); // v bytes += mat_dim * sizeof(double); // v
bytes += (mr + 1) * mr * sizeof(double); // y bytes += (double) (mr + 1) * mr * sizeof(double); // y
return bytes; return bytes;
} }

View File

@ -37,6 +37,7 @@
#include "kspace.h" #include "kspace.h"
#include "math_const.h" #include "math_const.h"
#include "math_extra.h" #include "math_extra.h"
#include "math_special.h"
#include "memory.h" #include "memory.h"
#include "modify.h" #include "modify.h"
#include "msm_dielectric.h" #include "msm_dielectric.h"
@ -60,6 +61,7 @@ using namespace LAMMPS_NS;
using namespace FixConst; using namespace FixConst;
using namespace MathExtra; using namespace MathExtra;
using namespace MathConst; using namespace MathConst;
using namespace MathSpecial;
enum { REAL2SCALED = 0, SCALED2REAL = 1 }; enum { REAL2SCALED = 0, SCALED2REAL = 1 };
@ -589,18 +591,18 @@ void FixPolarizeFunctional::set_arrays(int i)
double FixPolarizeFunctional::memory_usage() double FixPolarizeFunctional::memory_usage()
{ {
double bytes = 0; double bytes = 0;
bytes += num_induced_charges * num_induced_charges * sizeof(double); // inverse_matrix bytes += square(num_induced_charges) * sizeof(double); // inverse_matrix
bytes += num_induced_charges * num_induced_charges * sizeof(double); // Rww bytes += square(num_induced_charges) * sizeof(double); // Rww
bytes += num_induced_charges * num_induced_charges * sizeof(double); // G1ww bytes += square(num_induced_charges) * sizeof(double); // G1ww
bytes += num_induced_charges * num_induced_charges * sizeof(double); // ndotGww bytes += square(num_induced_charges) * sizeof(double); // ndotGww
bytes += num_induced_charges * num_induced_charges * sizeof(double); // G2ww bytes += square(num_induced_charges) * sizeof(double); // G2ww
bytes += num_induced_charges * num_induced_charges * sizeof(double); // G3ww bytes += square(num_induced_charges) * sizeof(double); // G3ww
bytes += num_induced_charges * sizeof(double); // qiRqwVector bytes += num_induced_charges * sizeof(double); // qiRqwVector
bytes += num_induced_charges * sizeof(double); // sum2G2wq bytes += num_induced_charges * sizeof(double); // sum2G2wq
bytes += num_induced_charges * sizeof(double); // sum1G2qw bytes += num_induced_charges * sizeof(double); // sum1G2qw
bytes += num_induced_charges * sizeof(double); // sum1G1qw_epsilon bytes += num_induced_charges * sizeof(double); // sum1G1qw_epsilon
bytes += num_induced_charges * sizeof(double); // sum2ndotGwq_epsilon bytes += num_induced_charges * sizeof(double); // sum2ndotGwq_epsilon
bytes += num_ions * num_induced_charges * sizeof(double); // G1qw_real bytes += (double)num_ions * num_induced_charges * sizeof(double); // G1qw_real
bytes += nmax * sizeof(int); // induced_charge_idx bytes += nmax * sizeof(int); // induced_charge_idx
bytes += nmax * sizeof(int); // ion_idx bytes += nmax * sizeof(int); // ion_idx
bytes += num_induced_charges * sizeof(double); // induced_charges bytes += num_induced_charges * sizeof(double); // induced_charges

View File

@ -705,7 +705,7 @@ void MLIAP_SO3::get_sbes_array(int nlocal, int *numneighs, double **rij, int lma
bigint ipair = 0; bigint ipair = 0;
bigint gindex; bigint gindex;
int findex = m_Nmax * (m_lmax + 1); int findex = m_Nmax * (m_lmax + 1);
int mindex = m_lmax + 1; const bigint mindex = m_lmax + 1;
for (int ii = 0; ii < nlocal; ii++) { for (int ii = 0; ii < nlocal; ii++) {
@ -721,10 +721,10 @@ void MLIAP_SO3::get_sbes_array(int nlocal, int *numneighs, double **rij, int lma
if (ri < SMALL) continue; if (ri < SMALL) continue;
pfac2 = pfac1 * ri; pfac2 = pfac1 * ri;
gindex = (ipair - 1) * findex; gindex = (ipair - 1) * findex;
for (i = 1; i < m_Nmax + 1; i++) { for (i = 1; i < m_Nmax + 1; i++) {
const bigint i1mindex = (bigint) (i - 1) * mindex;
x = cos((2 * i - 1) * pfac3); x = cos((2 * i - 1) * pfac3);
xi = pfac4 * (x + 1); xi = pfac4 * (x + 1);
@ -733,28 +733,27 @@ void MLIAP_SO3::get_sbes_array(int nlocal, int *numneighs, double **rij, int lma
sa = sinh(rb) / rb; sa = sinh(rb) / rb;
sb = (cosh(rb) - sa) / rb; sb = (cosh(rb) - sa) / rb;
m_sbes_array[gindex + (i - 1) * mindex + 0] = sa; m_sbes_array[gindex + i1mindex + 0] = sa;
m_sbes_array[gindex + (i - 1) * mindex + 1] = sb; m_sbes_array[gindex + i1mindex + 1] = sb;
for (j = 2; j < lmax + 1; j++) for (j = 2; j < lmax + 1; j++)
m_sbes_array[gindex + (i - 1) * mindex + j] = m_sbes_array[gindex + i1mindex + j] = m_sbes_array[gindex + i1mindex + j - 2] -
m_sbes_array[gindex + (i - 1) * mindex + j - 2] - (2 * j - 1) / rb * m_sbes_array[gindex + i1mindex + j - 1];
(2 * j - 1) / rb * m_sbes_array[gindex + (i - 1) * mindex + j - 1];
exts = m_sbes_array[gindex + (i - 1) * mindex + j - 2] - exts = m_sbes_array[gindex + (i - 1) * mindex + j - 2] -
(2 * j - 1) / rb * m_sbes_array[gindex + (i - 1) * mindex + j - 1]; (2 * j - 1) / rb * m_sbes_array[gindex + i1mindex + j - 1];
m_sbes_darray[gindex + (i - 1) * mindex + 0] = sb; m_sbes_darray[gindex + i1mindex + 0] = sb;
for (j = 1; j < lmax; j++) for (j = 1; j < lmax; j++)
m_sbes_darray[gindex + (i - 1) * mindex + j] = xi * m_sbes_darray[gindex + i1mindex + j] = xi *
(j * m_sbes_array[gindex + (i - 1) * mindex + j - 1] + (j * m_sbes_array[gindex + i1mindex + j - 1] +
(j + 1) * m_sbes_array[gindex + (i - 1) * mindex + j + 1]) / (j + 1) * m_sbes_array[gindex + i1mindex + j + 1]) /
(2 * j + 1); (2 * j + 1);
m_sbes_darray[gindex + (i - 1) * mindex + j] = xi * m_sbes_darray[gindex + (i - 1) * mindex + j] =
(j * m_sbes_array[gindex + (i - 1) * mindex + j - 1] + (j + 1) * exts) / (2 * j + 1); xi * (j * m_sbes_array[gindex + i1mindex + j - 1] + (j + 1) * exts) / (2 * j + 1);
m_sbes_darray[gindex + (i - 1) * mindex + 0] = xi * sb; m_sbes_darray[gindex + i1mindex + 0] = xi * sb;
} }
} }
} }
@ -797,14 +796,14 @@ void MLIAP_SO3::get_rip_array(int nlocal, int *numneighs, double **rij, int nmax
integrald = 0.0; integrald = 0.0;
for (i = 0; i < m_Nmax; i++) { for (i = 0; i < m_Nmax; i++) {
integral += m_g_array[(n - 1) * m_Nmax + i] * integral += m_g_array[(n - 1) * m_Nmax + i] *
m_sbes_array[(ipair - 1) * m_Nmax * (m_lmax + 1) + i * (m_lmax + 1) + l]; m_sbes_array[(ipair - 1) * m_Nmax * (m_lmax + 1) + (bigint) i * (m_lmax + 1) + l];
integrald += m_g_array[(n - 1) * m_Nmax + i] * integrald += m_g_array[(n - 1) * m_Nmax + i] *
m_sbes_darray[(ipair - 1) * m_Nmax * (m_lmax + 1) + i * (m_lmax + 1) + l]; m_sbes_darray[(ipair - 1) * m_Nmax * (m_lmax + 1) + (bigint) i * (m_lmax + 1) + l];
} }
m_rip_array[(ipair - 1) * m_nmax * (m_lmax + 1) + (n - 1) * (m_lmax + 1) + l] = m_rip_array[(ipair - 1) * m_nmax * (m_lmax + 1) + (bigint) (n - 1) * (m_lmax + 1) + l] =
integral * expfac; integral * expfac;
m_rip_darray[(ipair - 1) * m_nmax * (m_lmax + 1) + (n - 1) * (m_lmax + 1) + l] = m_rip_darray[(ipair - 1) * m_nmax * (m_lmax + 1) + (bigint) (n - 1) * (m_lmax + 1) + l] =
integrald * expfac; integrald * expfac;
} }
} }
@ -904,7 +903,7 @@ void MLIAP_SO3::spectrum(int nlocal, int *numneighs, int *jelems, double *wjelem
for (int n = 1; n < nmax + 1; n++) { for (int n = 1; n < nmax + 1; n++) {
int i = 0; int i = 0;
for (int l = 0; l < lmax + 1; l++) { for (int l = 0; l < lmax + 1; l++) {
r_int = m_rip_array[gindex + (n - 1) * (m_lmax + 1) + l]; r_int = m_rip_array[gindex + (bigint) (n - 1) * (m_lmax + 1) + l];
for (int m = -l; m < l + 1; m++) { for (int m = -l; m < l + 1; m++) {
@ -1005,7 +1004,7 @@ void MLIAP_SO3::spectrum_dxdr(int nlocal, int *numneighs, int *jelems, double *w
for (int ii = 0; ii < nlocal; ii++) { for (int ii = 0; ii < nlocal; ii++) {
totali = nmax * m_numYlms; totali = (bigint) nmax * m_numYlms;
for (bigint ti = 0; ti < totali; ti++) { for (bigint ti = 0; ti < totali; ti++) {
m_clisttot_r[ti] = 0.0; m_clisttot_r[ti] = 0.0;
m_clisttot_i[ti] = 0.0; m_clisttot_i[ti] = 0.0;
@ -1024,7 +1023,7 @@ void MLIAP_SO3::spectrum_dxdr(int nlocal, int *numneighs, int *jelems, double *w
r = sqrt(x * x + y * y + z * z); r = sqrt(x * x + y * y + z * z);
if (r < SMALL) continue; if (r < SMALL) continue;
totali = nmax * m_numYlms; totali = (bigint) nmax * m_numYlms;
for (bigint ti = 0; ti < totali; ti++) { for (bigint ti = 0; ti < totali; ti++) {
m_clist_r[ti] = 0.0; m_clist_r[ti] = 0.0;
@ -1044,7 +1043,7 @@ void MLIAP_SO3::spectrum_dxdr(int nlocal, int *numneighs, int *jelems, double *w
for (int n = 1; n < nmax + 1; n++) { for (int n = 1; n < nmax + 1; n++) {
int i = 0; int i = 0;
for (int l = 0; l < lmax + 1; l++) { for (int l = 0; l < lmax + 1; l++) {
r_int = m_rip_array[gindex + (n - 1) * (m_lmax + 1) + l]; r_int = m_rip_array[gindex + (bigint) (n - 1) * (m_lmax + 1) + l];
for (int m = -l; m < l + 1; m++) { for (int m = -l; m < l + 1; m++) {
@ -1057,7 +1056,7 @@ void MLIAP_SO3::spectrum_dxdr(int nlocal, int *numneighs, int *jelems, double *w
} }
} }
totali = nmax * m_numYlms; totali = (bigint) nmax * m_numYlms;
for (bigint tn = 0; tn < totali; tn++) { for (bigint tn = 0; tn < totali; tn++) {
m_clist_r[tn] = m_clist_r[tn] * double(weight); m_clist_r[tn] = m_clist_r[tn] * double(weight);
m_clist_i[tn] = m_clist_i[tn] * double(weight); m_clist_i[tn] = m_clist_i[tn] * double(weight);
@ -1174,9 +1173,10 @@ void MLIAP_SO3::spectrum_dxdr(int nlocal, int *numneighs, int *jelems, double *w
for (int n = 1; n < nmax + 1; n++) { for (int n = 1; n < nmax + 1; n++) {
int i = 0; int i = 0;
for (int l = 0; l < lmax + 1; l++) { for (int l = 0; l < lmax + 1; l++) {
r_int = m_rip_array[(idpair - 1) * m_nmax * (m_lmax + 1) + (n - 1) * (m_lmax + 1) + l]; r_int = m_rip_array[(idpair - 1) * m_nmax * (m_lmax + 1) +
r_int_temp = (bigint) (n - 1) * (m_lmax + 1) + l];
m_rip_darray[(idpair - 1) * m_nmax * (m_lmax + 1) + (n - 1) * (m_lmax + 1) + l]; r_int_temp = m_rip_darray[(idpair - 1) * m_nmax * (m_lmax + 1) +
(bigint) (n - 1) * (m_lmax + 1) + l];
for (int ii = 0; ii < 3; ii++) dr_int[ii] = r_int_temp * 2.0 * alpha * rvec[ii] / r; for (int ii = 0; ii < 3; ii++) dr_int[ii] = r_int_temp * 2.0 * alpha * rvec[ii] / r;