From 1ec54949673cd4b3af128e87585898834d5a6352 Mon Sep 17 00:00:00 2001 From: Mark Potse Date: Mon, 13 May 2024 12:55:40 +0200 Subject: [PATCH] use fgets() results when reading .mesh files, plus language fixes The .mesh reader uses fgets() to skip comments (until end of line) but did not check the result, so it would not have noticed anything in the unlikely case of a comment line longer than MMG5_FILESTR_LGTH = 128. This commit lets fgets be called repeatedly until EOL or EOF is encountered. This also avoids a compiler warning on each library. Using getline() would be easier but that is not standard C. --- src/mmg2d/inout_2d.c | 16 ++++++++++------ src/mmg3d/inout_3d.c | 30 +++++++++++++++++------------- src/mmgs/inout_s.c | 26 +++++++++++++++----------- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/mmg2d/inout_2d.c b/src/mmg2d/inout_2d.c index 1d311d3a0..685264ef6 100644 --- a/src/mmg2d/inout_2d.c +++ b/src/mmg2d/inout_2d.c @@ -84,7 +84,11 @@ int MMG2D_loadMesh(MMG5_pMesh mesh,const char *filename) { strcpy(chaine,"D"); while(fscanf(inm,"%127s",&chaine[0])!=EOF && strncmp(chaine,"End",strlen("End")) ) { if ( chaine[0] == '#' ) { - fgets(strskip,MMG5_FILESTR_LGTH,inm); + while(1){ // skip until end of line or file + char *s = fgets(strskip,MMG5_FILESTR_LGTH,inm); + if(!s) break; // nothing could be read + if(s[strlen(s)-1]=='\n') break; // end of line + } continue; } @@ -640,7 +644,7 @@ int MMG2D_loadGenericMesh(MMG5_pMesh mesh, MMG5_pSol met, MMG5_pSol sol, const c ier = MMG2D_loadMesh(mesh,tmp); if ( ier < 1 ) { break; } - /* Facultative metric */ + /* Optional metric */ if ( sol ) { MMG5_SAFE_MALLOC(soltmp,strlen(solnameptr)+1,char,return -1); strcpy(soltmp,solnameptr); @@ -960,7 +964,7 @@ int MMG2D_loadSol(MMG5_pMesh mesh,MMG5_pSol sol,const char *filename) { fseek(inm,posnp,SEEK_SET); if ( sol->ver == 1 ) { - /* Simple precision */ + /* Single precision */ for (k=1; k<=sol->np; k++) { if ( MMG2D_readFloatSol(sol,inm,bin,iswp,k) < 0 ) return -1; } @@ -1041,7 +1045,7 @@ int MMG2D_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) { for ( j=0; jdim = 2; - /* For binary file, we read the verson inside the file */ + /* For binary files, we read the verson inside the file */ if ( ver ) psl->ver = ver; } MMG5_SAFE_FREE(type); @@ -1069,7 +1073,7 @@ int MMG2D_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) { fseek(inm,posnp,SEEK_SET); if ( (*sol)[0].ver == 1 ) { - /* Simple precision */ + /* Single precision */ for (k=1; k<=mesh->np; k++) { for ( j=0; jtag &= ~MG_NUL; } - /* Possibly switch 2 vertices number so that each tet is positively oriented */ + /* Possibly switch 2 vertex numbers so that each tet is positively oriented */ if ( MMG5_orvol(mesh->point,pt->v) < 0.0 ) { /* mesh->xt temporary used to count reoriented tetra */ mesh->xt++; @@ -1233,7 +1237,7 @@ int MMG3D_loadGenericMesh(MMG5_pMesh mesh, MMG5_pSol met, MMG5_pSol sol, const c ier = MMG3D_loadMesh(mesh,tmp); if ( ier < 1 ) { break; } - /* Facultative metric */ + /* Optional metric */ if ( sol ) { MMG5_SAFE_MALLOC(soltmp,strlen(solnameptr)+1,char,return -1); strcpy(soltmp,solnameptr); @@ -1286,7 +1290,7 @@ int MMG3D_saveMesh(MMG5_pMesh mesh, const char *filename) { return 0; } - /*entete fichier*/ + /* file header */ binch=0; bpos=10; if(!bin) { strcpy(&chaine[0],"MeshVersionFormatted 2\n"); @@ -2041,7 +2045,7 @@ int MMG3D_saveMesh(MMG5_pMesh mesh, const char *filename) { } } - /*fin fichier*/ + /* end of file */ if(!bin) { strcpy(&chaine[0],"\n\nEnd\n"); fprintf(inm,"%s",chaine); @@ -2173,13 +2177,13 @@ int MMG3D_loadSol(MMG5_pMesh mesh,MMG5_pSol met, const char *filename) { return ier; } - /* Allocate and store the header informations for each solution */ + /* Allocate and store the header information for each solution */ if ( !MMG3D_Set_solSize(mesh,met,MMG5_Vertex,mesh->np,type[0]) ) { fclose(inm); MMG5_SAFE_FREE(type); return -1; } - /* For binary file, we read the verson inside the file */ + /* For binary files, we read the verson inside the file */ if ( ver ) met->ver = ver; MMG5_SAFE_FREE(type); @@ -2189,7 +2193,7 @@ int MMG3D_loadSol(MMG5_pMesh mesh,MMG5_pSol met, const char *filename) { fseek(inm,posnp,SEEK_SET); if ( met->ver == 1 ) { - /* Simple precision */ + /* Single precision */ for (k=1; k<=mesh->np; k++) { if ( MMG5_readFloatSol3D(met,inm,bin,iswp,k) < 0 ) return -1; } @@ -2254,7 +2258,7 @@ int MMG3D_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) { for ( j=0; jver = ver; } MMG5_SAFE_FREE(type); @@ -2282,7 +2286,7 @@ int MMG3D_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) { fseek(inm,posnp,SEEK_SET); if ( (*sol)[0].ver == 1 ) { - /* Simple precision */ + /* Single precision */ for (k=1; k<=mesh->np; k++) { for ( j=0; j 0 ) { rewind(inm); fseek(inm,posnq,SEEK_SET); @@ -888,7 +892,7 @@ int MMGS_saveMesh(MMG5_pMesh mesh, const char* filename) { fprintf(stdout," %%%% %s OPENED\n",data); MMG5_SAFE_FREE(data); - /*entete fichier*/ + /* file header */ if(!bin) { strcpy(&chaine[0],"MeshVersionFormatted 2\n"); fprintf(inm,"%s",chaine); @@ -1281,7 +1285,7 @@ int MMGS_saveMesh(MMG5_pMesh mesh, const char* filename) { if ( nn+ng ) fprintf(stdout," NUMBER OF NORMALS %8" MMG5_PRId " TANGENTS %6" MMG5_PRId "\n",nn,ng); } - /*fin fichier*/ + /* end of file */ if(!bin) { strcpy(&chaine[0],"\n\nEnd\n"); fprintf(inm,"%s",chaine); @@ -1334,7 +1338,7 @@ int MMGS_loadSol(MMG5_pMesh mesh,MMG5_pSol met,const char* filename) { return -1; } - /* #MMG5_loadSolHeader function reads only solution at vertices so we don't + /* #MMG5_loadSolHeader function reads only solutions at vertices so we don't have to check the entites on which the metric applies */ int entities = MMG5_Vertex; ier = MMG5_chkMetricType(mesh,type,&entities,inm); @@ -1343,7 +1347,7 @@ int MMGS_loadSol(MMG5_pMesh mesh,MMG5_pSol met,const char* filename) { return ier; } - /* Allocate and store the header informations for each solution */ + /* Allocate and store the header information for each solution */ if ( !MMGS_Set_solSize(mesh,met,MMG5_Vertex,mesh->np,type[0]) ) { fclose(inm); MMG5_SAFE_FREE(type); @@ -1424,7 +1428,7 @@ int MMGS_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) { for ( j=0; jnp,type[j]) ) { MMG5_SAFE_FREE(type); fclose(inm); return -1; } - /* For binary file, we read the verson inside the file */ + /* For binary files, we read the verson inside the file */ if ( ver ) psl->ver = ver; } MMG5_SAFE_FREE(type); @@ -1451,7 +1455,7 @@ int MMGS_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) { fseek(inm,posnp,SEEK_SET); if ( (*sol)[0].ver == 1 ) { - /* Simple precision */ + /* Single precision */ for (k=1; k<=mesh->np; k++) { for ( j=0; j