Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possibly invalid pointer #231

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/mmg2d/analys_2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ int MMG2D_setadj(MMG5_pMesh mesh, int8_t init_cc) {
int16_t tag;
int8_t i,ii,i1,i2;

if ( !mesh->nt ) {
return 1;
}

if ( abs(mesh->info.imprim) > 5 || mesh->info.ddebug )
fprintf(stdout," ** SETTING TOPOLOGY\n");

Expand Down
4 changes: 2 additions & 2 deletions src/mmg2d/hash_2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* \param mesh pointer toward the mesh
* \return 1 if success, 0 if fail
*
* Create adjacency relations between the triangles dein the mesh
* Create adjacency relations between the triangles in the mesh
*
*/
int MMG2D_hashTria(MMG5_pMesh mesh) {
Expand All @@ -40,7 +40,7 @@ int MMG2D_hashTria(MMG5_pMesh mesh) {
unsigned int key;

if ( mesh->adja ) return 1;
if ( !mesh->nt ) return 0;
if ( !mesh->nt ) return 1;

/* memory alloc */
MMG5_SAFE_CALLOC(hcode,mesh->nt+1,MMG5_int,return 0);
Expand Down
4 changes: 4 additions & 0 deletions src/mmg2d/length_2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ int MMG2D_prilen(MMG5_pMesh mesh,MMG5_pSol sol) {
ibmax = 0;
nullEdge = 0;

if ( !mesh->nt ) {
return 0;
}

for (k=0; k<9; k++) hl[k] = 0;

for (k=1; k<=mesh->nt; k++) {
Expand Down
4 changes: 4 additions & 0 deletions src/mmg2d/quality_2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ int MMG2D_outqua(MMG5_pMesh mesh,MMG5_pSol met) {
MMG5_int k,iel,ok,nex;
static int8_t mmgWarn0;

if ( !mesh->nt ) {
return 1;
}

/* Compute triangle quality*/
for (k=1; k<=mesh->nt; k++) {
pt = &mesh->tria[k];
Expand Down
8 changes: 6 additions & 2 deletions src/mmg2d/zaldy_2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ void MMG2D_delPt(MMG5_pMesh mesh,MMG5_int ip) {
ppt->tmp = mesh->npnil;

mesh->npnil = ip;
if ( ip == mesh->np ) mesh->np--;
if ( ip == mesh->np ) {
while ( (!MG_VOK((&mesh->point[mesh->np]))) && mesh->np ) mesh->np--;
}
}

void MMG5_delEdge(MMG5_pMesh mesh,MMG5_int iel) {
Expand Down Expand Up @@ -119,7 +121,9 @@ int MMG2D_delElt(MMG5_pMesh mesh,MMG5_int iel) {
memset(&mesh->adja[iadr],0,3*sizeof(MMG5_int));

mesh->nenil = iel;
if ( iel == mesh->nt ) mesh->nt--;
if ( iel == mesh->nt ) {
while ( (!MG_EOK((&mesh->tria[mesh->nt]))) && mesh->nt ) mesh->nt--;
}
return 1;
}

Expand Down
3 changes: 3 additions & 0 deletions src/mmg3d/anisomovpt_3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ int MMG5_movbdyregpt_ani(MMG5_pMesh mesh, MMG5_pSol met, MMG3D_pPROctree PROctre

/** Step 5 : come back to original problem, compute patch in triangle iel and
* check that geometric approx has not been degraded too much */
// Remark: if we call following function with a pointer for n, we have to set
// the pointer again after the function call as it may invalidate it if it
// reallocates the xpoint array
nxp = MMG3D_movbdyregpt_geom(mesh,lists,kel,ip0,n,lambda,o,no);
if ( nxp < 0 ) {
return -1;
Expand Down
12 changes: 10 additions & 2 deletions src/mmg3d/movpt_3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ int MMG3D_rotate_surfacicBall(MMG5_pMesh mesh,MMG5_int *lists,int ilists,MMG5_in
* Compute the Bezier patch at element \a lists[kel], compute the new point
* coordinates, normal and tangent and check the geometric approximation.
*
* \warning may invalidate n if it is a pointer toward the xpoint array
*/
int MMG3D_movbdyregpt_geom(MMG5_pMesh mesh,MMG5_int *lists,const MMG5_int kel,
const MMG5_int ip0,double n[3],double lambda[3],double o[3],
Expand Down Expand Up @@ -629,7 +630,7 @@ int MMG5_movbdyregpt_iso(MMG5_pMesh mesh, MMG5_pSol met, MMG3D_pPROctree PROctre
MMG5_pPoint p0;
MMG5_Tria tt;
MMG5_pxPoint pxp;
double *n,r[3][3],lispoi[3*MMG3D_LMAX+1],ux,uy,det2d;
double n[3],r[3][3],lispoi[3*MMG3D_LMAX+1],ux,uy,det2d;
double detloc,oppt[2],step,lambda[3];
double ll,m[2],o[3],no[3];
double calold,calnew,caltmp,callist[MMG3D_LMAX+2];
Expand All @@ -650,7 +651,11 @@ int MMG5_movbdyregpt_iso(MMG5_pMesh mesh, MMG5_pSol met, MMG3D_pPROctree PROctre
p0 = &mesh->point[ip0];
assert( p0->xp && !MG_EDG(p0->tag) );

n = &(mesh->xpoint[p0->xp].n1[0]);
/* Don't use a pointer for n to avoid issues with the reallocation of the
* xpoint array and invalid pointers (see ls-CenIn-DisOut-8 test of ParMmg) */
n[0] = mesh->xpoint[p0->xp].n1[0];
n[1] = mesh->xpoint[p0->xp].n1[1];
n[2] = mesh->xpoint[p0->xp].n1[2];

/** Step 1 : rotation matrix that sends normal n to the third coordinate vector of R^3 */
if ( !MMG5_rotmatrix(n,r) ) return 0;
Expand Down Expand Up @@ -727,6 +732,9 @@ int MMG5_movbdyregpt_iso(MMG5_pMesh mesh, MMG5_pSol met, MMG3D_pPROctree PROctre
lambda[0] = 1.0 - lambda[1] - lambda[2];

/** Step 5 : come back to original problem, and compute patch in triangle iel */
// Remark: if we call following function with a pointer for n, we have to set
// the pointer again after the function call as it may invalidate it if it
// reallocates the xpoint array
nxp = MMG3D_movbdyregpt_geom(mesh,lists,kel,ip0,n,lambda,o,no);
if ( nxp < 0 ) {
return -1;
Expand Down
4 changes: 2 additions & 2 deletions src/mmg3d/zaldy_3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void MMG3D_delPt(MMG5_pMesh mesh,MMG5_int ip) {
ppt->tmp = mesh->npnil;
mesh->npnil = ip;
if ( ip == mesh->np ) {
while ( !MG_VOK((&mesh->point[mesh->np])) ) mesh->np--;
while ( (!MG_VOK((&mesh->point[mesh->np]))) && mesh->np ) mesh->np--;
}
}

Expand Down Expand Up @@ -135,7 +135,7 @@ int MMG3D_delElt(MMG5_pMesh mesh,MMG5_int iel) {
memset(&mesh->adja[iadr],0,4*sizeof(MMG5_int));
mesh->nenil = iel;
if ( iel == mesh->ne ) {
while ( !MG_EOK((&mesh->tetra[mesh->ne])) ) mesh->ne--;
while ( (!MG_EOK((&mesh->tetra[mesh->ne]))) && mesh->ne ) mesh->ne--;
}
return 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mmgs/zaldy_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void MMGS_delPt(MMG5_pMesh mesh,MMG5_int ip) {
ppt->tmp = mesh->npnil;
mesh->npnil = ip;
if ( ip == mesh->np ) {
while ( !MG_VOK((&mesh->point[mesh->np])) ) mesh->np--;
while ( (!MG_VOK((&mesh->point[mesh->np]))) && mesh->np ) mesh->np--;
}
}

Expand Down Expand Up @@ -104,7 +104,7 @@ int MMGS_delElt(MMG5_pMesh mesh,MMG5_int iel) {
memset(&mesh->adja[3*(iel-1)+1],0,3*sizeof(MMG5_int));
mesh->nenil = iel;
if ( iel == mesh->nt ) {
while ( !MG_EOK((&mesh->tria[mesh->nt])) ) mesh->nt--;
while ( (!MG_EOK((&mesh->tria[mesh->nt]))) && mesh->nt ) mesh->nt--;
}
return 1;
}
Expand Down