C语言程序参数数量不符时触发core dump问题求助
First, let's tackle the core dump issue you're facing—it's caused by accessing out-of-bounds elements in the argv array before validating the number of arguments. Here's the breakdown:
In your original code, you immediately try to read argv[2] and argv[3] to set Min and Max:
int Min=atoi(argv[2]); int Max=atoi(argv[3]); if(Min>Max || argc!=4){ DisplayHelp(); }
When you pass 2 or 3 arguments, argv[2] (or argv[3]) doesn't exist—argv only contains elements up to argv[argc-1]. Accessing these invalid memory locations triggers a segmentation fault (core dump).
Step 1: Fix the Core Dump
Move the argument count check before accessing argv[2] and argv[3]. Also, add validation for valid integer values for Min and Max (since atoi returns 0 if the input isn't a number, which could break your length checks).
Here's the corrected start to your main function:
int main(int argc, char * argv[] ){ char * type1="$1$"; char * type6="$6$"; char * salt="$"; char * mdresult; char * sharesult; // Added: missing declaration for sharesult char encyption_scheme[20]; FILE * fp; FILE * fp1; char input1 [50]; int length; int line=0; int line2=0; int Min, Max; // First validate argument count if(argc!=4){ DisplayHelp(); } // Now safely access argv[2] and argv[3] Min=atoi(argv[2]); Max=atoi(argv[3]); // Validate min/max values meet requirements if(Min <=1 || Max < Min){ printf("Invalid min/max values!\n"); DisplayHelp(); } // ... rest of your code }
Step 2: Fix Other Critical Bugs
Your code has a few other issues that will cause failures even with correct arguments:
- Undefined variable:
sharesultis used but never declared—addchar *sharesult;alongsidemdresult. - Inefficient file I/O: Opening and closing
Filename.txtevery iteration is slow and risky. Open it once before processing and close it after:// Open output file once before the loop fp1=fopen("Filename.txt","a"); if (fp1==NULL) { perror("Failed to open output file"); exit(-1); } while(fgets(input1,50,fp)){ // ... your processing logic ... fprintf(fp1, "%s:%s\n", input1, mdresult); fprintf(fp1, "%s:%s\n", input1, sharesult); } // Close output file after processing all lines fclose(fp1); - Check for
cryptfailures: Thecryptfunction can returnNULLif the encryption scheme is invalid or there's an error. Always validate the result:mdresult = crypt(input1, encyption_scheme); if(mdresult == NULL){ perror("MD5 encryption failed"); continue; // Skip this entry or exit, depending on your needs } // Same check for SHA-512 result sharesult = crypt(input1, encyption_scheme); if(sharesult == NULL){ perror("SHA-512 encryption failed"); continue; }
Step 3: Optional Robustness Improvements
- Buffer safety: Use
strncatinstead ofstrcatto avoid potential buffer overflow inencyption_scheme:strcpy(encyption_scheme, type1); strncat(encyption_scheme, salt, sizeof(encyption_scheme) - strlen(encyption_scheme) - 1); - Safe newline trimming: Your current code trims the newline without checking if the line is empty, which could lead to writing to invalid memory. Add a check:
length=strlen(input1); if(length > 0 && input1[length-1] == '\n'){ input1[length-1] = '\0'; length--; }
After making these changes, your program will no longer core dump with incorrect argument counts, and will be more reliable overall.
内容的提问来源于stack exchange,提问作者Syuuバカ

