You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言程序参数数量不符时触发core dump问题求助

Fixing Core Dump & Improving Your Word Encryption Program

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: sharesult is used but never declared—add char *sharesult; alongside mdresult.
  • Inefficient file I/O: Opening and closing Filename.txt every 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 crypt failures: The crypt function can return NULL if 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 strncat instead of strcat to avoid potential buffer overflow in encyption_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バカ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:07:13