Thread: Stack Smashing error

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    1

    Stack Smashing error

    Does anyone know why my code has an error of "stack smashing detected"? And how I would go about fixing it? Thanks

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main(void) {
       
       char abc[26];
       FILE* encryptFile;
       char code[1000];
       int form = 0;
       char ch;
       
       encryptFile = fopen("encrypted.txt", "r");
       do {
          ch = fgetc(encryptFile);
          code[form] = ch;
          form+=1;
       }
       while (ch != EOF);
       fclose(encryptFile);
       encryptFile = fopen("substitution.txt", "r");
       
       for(int i=0;i<26;i++){
          char ch = fgetc(encryptFile);
          abc[ch-97]=(char)(i+97);
       }
       
       fclose(encryptFile);
       encryptFile = fopen("decrypted.txt", "w");
       char first[form];
       for(int i=0; i<form-1;i++){
          ch = code[i];
          if(ch >= 97 && ch <= 122)
          first[i]=abc[ch-97];
          else if(ch >= 65 && ch <= 90){
             first[i]=toupper(abc[tolower(ch)-97]);
          }
          
          else 
          first[i]=ch;
          fprintf(encryptFile, "%c",first[i]);
       }
          
     
       return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char code[1000];
    How big is encrypted.txt?
    If it's more than 1000 chars, you have a problem.


    > And how I would go about fixing it?
    You don't need your code array, nor your first array.
    Code:
    FILE* encryptFile = fopen("encrypted.txt", "r");
    FILE* decryptFile = fopen("decrypted.txt", "w");
    int ch;
    while ( (ch=fgetc(encryptFile)) != EOF ) {
        if(ch >= 97 && ch <= 122)
            ch=abc[ch-97];
        else if(ch >= 65 && ch <= 90){
            ch=toupper(abc[tolower(ch)-97]);
        }
        fputc(ch, decryptFile);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    I did a quick code review for you - see added comments.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main(void) {
    
    
       char abc[26];
       FILE* encryptFile;
       char code[1000];
       int form = 0;
       char ch;
    
    
       encryptFile = fopen("encrypted.txt", "r");
       // You should check that the file has opened
       do {
          ch = fgetc(encryptFile);
          // If "encrypted.txt" is more than 1000 bytes, you will trash memory
          code[form] = ch;
          form+=1;
       }
       while (ch != EOF);
    
    
       fclose(encryptFile);
    
    
       encryptFile = fopen("substitution.txt", "r");
       // You should check that the file has opened
    
    
       for(int i=0;i<26;i++){
          char ch = fgetc(encryptFile);
          // If your file has any characters < 97 or > 97+26 you will trash memory
          abc[ch-97]=(char)(i+97); // Consider using characcter constants
       }
    
    
       fclose(encryptFile);
       encryptFile = fopen("decrypted.txt", "w");
       // You should check that the file has opened
    
    
       char first[form];
       for(int i=0; i<form-1;i++){  // You most probably want "i<form", not "i<form-1"
          ch = code[i];
          if(ch >= 97 && ch <= 122) // Consider using character constants here
          first[i]=abc[ch-97];
          else if(ch >= 65 && ch <= 90){ // Consider using character constants
             first[i]=toupper(abc[tolower(ch)-97]); // Consider using chacter constants
          }
    
    
          else
          first[i]=ch;
          fprintf(encryptFile, "%c",first[i]);
       }
    
    
       return 0;
    }
    Also there are some weird indentation in the last loop. It would pay to make the nesting clear.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
     
    void init_alpha(char *alpha)
    {
        const char *filename = "substitution.txt";
        FILE *fin = fopen(filename, "r");
        if (!fin)
        {
            perror(filename);
            exit(EXIT_FAILURE);
        }
     
        memset(alpha, '-', 26);
     
        for (int i = 0; i < 26; ++i)
        {
            int ch = fgetc(fin);
            if (ch == EOF)
            {
                fprintf(stderr, "Error: less than 26 chars in subsitution file.\n");
                exit(EXIT_FAILURE);
            }
     
            if (ch >= 'a' && ch <= 'z')
                alpha[ch - 'a'] = i + 'a';
            else if (ch >= 'A' && ch <= 'Z')
                alpha[ch - 'A'] = i + 'a';
            else
            {
                fprintf(stderr, "Error: bad character in substitution file.\n");
                exit(EXIT_FAILURE);
            }
        }
        fclose(fin);
     
        if (memchr(alpha, '-', 26))
        {
            fprintf(stderr, "Error: not all characters in substitution file.\n");
            exit(EXIT_FAILURE);
        }
    }
     
    int main()
    {
        const char *filein  = "encrypted.txt";
        const char *fileout = "decrypted.txt";
     
        char alpha[26];
        init_alpha(alpha);
     
        FILE *fin = fopen(filein, "r");
        if (!fin)
        {
            perror(filein);
            exit(EXIT_FAILURE);
        }
     
        FILE *fout = fopen(fileout, "w");
        if (!fout)
        {
            perror(fileout);
            exit(EXIT_FAILURE);
        }
     
        for (int ch; (ch = fgetc(fin)) != EOF; )
        {
            if (ch >= 'a' && ch <= 'z')
                ch = alpha[ch - 'a'];
            else if (ch >= 'A' && ch <= 'Z')
                ch = toupper(alpha[ch - 'A']);
     
            fputc(ch, fout);
        }
     
        fclose(fout);
        fclose(fin);
        return 0;
    }
    Last edited by john.c; 02-16-2022 at 12:27 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Smashing error
    By sanddune008 in forum C Programming
    Replies: 2
    Last Post: 06-12-2013, 10:40 PM
  2. stack smashing error
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2008, 03:37 PM
  3. stack smashing error
    By spank in forum Linux Programming
    Replies: 2
    Last Post: 05-24-2007, 02:07 PM
  4. smashing the stack
    By rohit in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 07:06 AM
  5. smashing the stack
    By rohit in forum Linux Programming
    Replies: 1
    Last Post: 10-07-2002, 02:47 AM

Tags for this Thread