From 1171acf73c4da14db3f46d3111a27b5835915415 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Mon, 30 Jan 2012 11:30:00 +0100 Subject: [PATCH] getopt failure: now fails when reading more than $file.size from $file. --- scan/scan.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/scan/scan.c b/scan/scan.c index 04e7398..aff36b0 100755 --- a/scan/scan.c +++ b/scan/scan.c @@ -29,7 +29,8 @@ void usage () { fprintf(stderr, "Compile with XDEBUG or without DEBUG to get past this limit.\n"); fprintf(stderr, "You might want to compile this without the DEBUG flag to get less noice.\n"); #endif - fprintf(stderr, "\n"); + fprintf(stderr, "\nErrorcodes:\n"); + fprintf(stderr, "\t0: Everything went OK.\n\t1: General error.\n\t2: Getopt/wrong usage error.\n\t3: Inputfile error.\n\t4: Memory error.\n\n"); } @@ -113,7 +114,8 @@ int main(int argc, char *argv[]){ file = NULL; struct stat st; stat(filename, &st); - if (size <= 0 || size > st.st_size){ + + if (size == 0){ size = st.st_size; } #ifdef DEBUG @@ -121,9 +123,17 @@ int main(int argc, char *argv[]){ if (size > 64) size = 64; #endif #endif + if (size < 0 || size > st.st_size){ + if(rank == 0){ + fprintf (stderr, "[%d/%d:%s] Cannot read %li numbers from \"%s\" - only has %li bytes.\n",rank,nodes,name,size,filename,st.st_size); + usage(); + } + MPI_Finalize(); + exit(3); + } }else{ if(rank == 0){ - fprintf (stdout, "[%d/%d:%s] File %s does not exist.\n",rank,nodes,name,filename); + fprintf (stderr, "[%d/%d:%s] File %s does not exist.\n",rank,nodes,name,filename); usage(); } MPI_Finalize(); @@ -134,18 +144,18 @@ int main(int argc, char *argv[]){ if(databuf == NULL) { fprintf(stdout, "[%d/%d:%s] malloc for databuf failed.\n",rank,nodes,name); MPI_Finalize(); - exit(2); + exit(4); } blocksize = size/nodes; if(blocksize*nodes < size){ blocksize++; if(rank == 0) - fprintf(stdout, "[%d/%d:%s] perfect split impossible: n:%d, s:%li -> bs:%li (off: %li)\n",rank,nodes,name,nodes,size,blocksize,(nodes*blocksize)-size); + fprintf(stderr, "[%d/%d:%s] perfect split impossible: n:%d, s:%li -> bs:%li (off: %li)\n",rank,nodes,name,nodes,size,blocksize,(nodes*blocksize)-size); } if(rank == 0){ - fprintf(stdout, "[%d/%d:%s] s: %li n: %d bs: %li n*b: %li n*(b+1): %li\n",rank,nodes,name,size,nodes,blocksize,nodes*blocksize, nodes*(blocksize+1)); + fprintf(stdout, "[%d/%d:%s] INFO s: %li n: %d bs: %li n*b: %li n*(b+1): %li\n",rank,nodes,name,size,nodes,blocksize,nodes*blocksize, nodes*(blocksize+1)); /* read file. */ if ((file = fopen(filename, "r"))) { @@ -154,7 +164,7 @@ int main(int argc, char *argv[]){ } fclose(file); }else{ - fprintf (stdout, "[%d/%d:%s] File %s could not be read.\n",rank,nodes,name,filename); + fprintf (stderr, "[%d/%d:%s] File %s could not be read.\n",rank,nodes,name,filename); usage(); MPI_Finalize(); exit(3); -- 2.43.0