title: malloc(): corrupted top size
body: <p>I am working on a program that decrypts some line of text in a file. First, another source code I created asks for shift and some text. Source code encrypts the text and writes it into a file. </p>
  
  <p>Then, I try to decrypt the specified file using the below code -another source code. Name of the file is obtained. From file name, shift is obtained. Every character written inside the file is copied, shifted by the value of shift with the aid of the function caesar_decrypt -reversing caesar encrypt which I also have. However, given characters must be of ascii value between 32 and 127 to be transformed to another character of ascii value between 32 and 127. </p>
  
  <p>I defined my modulo function encapsulating the remainder operator -modulo of positive numbers- and modulo of negative numbers. I also defined strip function which works the same as strip function in python does. Aside from spaces, it removes all literals. I also have getIn(), which is input() of python.</p>
  
  <p>I tried to debug by printing and using gdb. No effective result. I have researched about this topic in stackoverflow. Found one entry. Answer did not address my specific issue.</p>
  
  <p>NOTE: I copied the whole program since I thought you would need  the necessary detailed information.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;math.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  
  /*GETIN FUNCTION*/
  char* getIn() 
  {
      char *line = NULL, *tmp = NULL;
      size_t size = 0, index = 0;
      int ch = EOF;
  
      while (ch) 
      {
          ch = getc(stdin);
  
          /* Check if we need to stop. */
          if (ch == EOF || ch == '\n')
          ch = 0;
  
          /* Check if we need to expand. */
          if (size &lt;= index) 
          {
              size += sizeof(char);
              tmp = realloc(line, size);
              if (!tmp) 
              {
                  free(line);
                  line = NULL;
                  break;
              }
          line = tmp;
          }
  
      /* Actually store the thing. */
      line[index++] = ch;
      }
  
      return line;
  }
  /*FUNCTION THAT CONVERTS TO NUMERIC CHARACTER, PRECURSOR OF DIGITS OF SHIFT*/
  char make_numeric(char ch)
  {
      if(ch&lt;91 &amp;&amp; ch&gt;63) return ch-16;
      if(ch&lt;123 &amp;&amp; ch&gt;95) return ch-48;
      if(ch&lt;58 &amp;&amp; ch&gt;47) return ch;
      return ch+16;
  }
  
   /*STRIP FUNCTION*/
  char* strip(char* str,int length)
  {
      char *start=str,*end=&amp;str[length-1];
  
      while(*start&gt;0 &amp;&amp; *start&lt;33 || *start==127) ++start;
      if(!*start) return start;
  
      while(*end&gt;=0 &amp;&amp; *end&lt;33 || *end==127) --end;
      ++end;
      *end=0;
  
      return start;
  }
  /*DECRYPTOR FUNCTION*/
  char *caesar_decrypt(char *message_to_decrypt, void *shift)
  {   
      int length=strlen(message_to_decrypt),i;
      char* decrypted_message;
  
      if(!message_to_decrypt) return NULL;
  
      if(!(decrypted_message=calloc(length+1,sizeof(char)))) return NULL;
  
      for(i=0;i&lt;length;++i)
      {
      decrypted_message[i]=mod((message_to_decrypt[i]-33-*((int*)shift)),94)+33;
      }
  
  return decrypted_message;
  }   
  
  /*THE MAIN PROGRAM*/
  int main()
  {
  
  int shift,len_chshift,init,len_filename,i=0;
  char *chshift,*line=NULL,*decrypted_line,*stripped_line,chinit,chlen_chshift;
  char *filename=NULL,*newfilename=NULL;
  FILE *oldfile=NULL;
  FILE *newfile=NULL;
  size_t lenline,len=0;
  
  printf("-9");
  /*FILENAME*/
  printf("Enter the file name: ");
  /*printf("-8");*/
  filename=getIn();
  /*printf("-7");*/
  if(!access(filename,F_OK))
  {
      len_filename=strlen(filename);
      /*printf("-6");*/
      i=len_filename;
      while(filename[i]!='.') --i;        
      chlen_chshift=filename[i-1];
      chlen_chshift=make_numeric(chlen_chshift);
      len_chshift=chlen_chshift-48;
      /*printf("-5");*/
      chshift=calloc(len_chshift+1,1);
      /*NEWFILENAME*/
      newfilename=calloc(i+1,1);
      /*printf("-4");*/
      strncpy(newfilename,filename,i);
      /*printf("-3");*/
      strcat(newfilename,"(decrypted).txt");
      /*printf("-2");*/
      chinit=make_numeric(filename[0]);
      init=chinit-48;
      /*SHIFT*/
      i=0;
      /*printf("-1");*/
      while(i!=len_chshift)
      {
          chshift[i]=make_numeric(filename[(i+1)*init]);
          ++i;
      }
      /*printf("0");*/
      shift=atoi(chshift);
  
      /*printf("1");*/
      if(!(oldfile=fopen(filename,"r")))
      {
          perror("Error");
          if(newfilename) free(newfilename);
          if(filename) free(filename);
          if(chshift) free(chshift);
          exit(1);
      }
      /*printf("2");*/
  
      if(!(newfile=fopen(newfilename,"+ab")))
      {
          perror("Error");
          if(newfilename) free(newfilename);
          if(filename) free(filename);
          if(chshift) free(chshift);
          fclose(oldfile);
          exit(1);
      }
      while ((lenline = getline(&amp;line, &amp;len, oldfile)) != -1)
      {
          stripped_line=strip(line,lenline);
          decrypted_line=caesar_decrypt(stripped_line, &amp;shift);
          if(!decrypted_line)
          {
              printf("Could not allocate memory\n");
              if(newfilename) free(newfilename);
              if(filename) free(filename);
              if(chshift) free(chshift);
              exit(1);
          }
  
          fprintf(newfile,"%s\n",decrypted_line);
  
          if(decrypted_line) free(decrypted_line);
          decrypted_line=NULL;
  
          free(line);
          line=NULL;
          stripped_line=NULL;
          decrypted_line=NULL;
      }
      free(line);
      line=NULL;
  }
  else
  {
      printf("Cannot access the file.");
      if(filename) free(filename);
      exit(0);
  }
  
  if(newfilename) free(newfilename);
  if(filename) free(filename);
  if(chshift) free(chshift);
  
  fclose(oldfile);
  fclose(newfile);
  return 0;
  }
  </code></pre>
  
  <p>The program is required to create a file, context of which is the decrypted clone of the other file, name of which is provided in the beginning by the user. However, it throws, just before the first fopen:</p>
  
  <pre><code>malloc(): corrupted top size
  Aborted (core dumped)
  </code></pre>
  
  <p>When I uncomment every printf line, it gives the same error between the lines of getIn() and printf("-7");. When printfs are commented, I observe that filename holds the written value, which is returned by getIn(). Therefore, I do not think getIn() is the problem.</p>
  
  <p><strong>EDIT(1)</strong>: I added 2 headers that were missing, but still raising the same error. </p>
  
  <p><strong>EDIT(2)</strong>: I removed all unnecessary headers.</p>
  
  <p><strong>EDIT(3)</strong>: Since I am requested, I will try to illuminate by an example: Suppose I have a file named</p>
  
  <pre><code>2/b*A".txt
  </code></pre>
  
  <p>with two lines of code</p>
  
  <pre><code>%52
  abcd
  </code></pre>
  
  <p>I enter the file name into program.</p>
  
  <p>1)The very last character before <strong>.</strong> is <strong>"</strong>, converted into number <strong>2</strong>. We understand shift is a two digit number.</p>
  
  <p>2)The first character is the initiator, which is <strong>2</strong> , is the number of characters we need to jump repeatedly. We will jump two characters twice since we have a shift number of two digits.</p>
  
  <p>3)We have, now characters b and A, converted to 2 and 1 respectively. Our shift is 21. The very first character <strong>%</strong>(37) is shifted backwards to <strong>n</strong>(110) and so on, we need to create a new file with the content: </p>
  
  <pre><code>n~#
  LMNO
  </code></pre>
  
  <p>I appreciate any help. Thank you in advance. In addition, the variables I mentioned are any arbitrary characters between specific intervals. For details, check the source code,or I suggest you to work locally, since I provided with the whole program.</p>
  
  <p>For ascii codes:
  <a href="https://www.asciitable.com/" rel="noreferrer">https://www.asciitable.com/</a></p>
  
  <p>If you are interested in Caesar's cipher:
  <a href="https://en.wikipedia.org/wiki/Caesar_cipher" rel="noreferrer">https://en.wikipedia.org/wiki/Caesar_cipher</a></p>