Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6943127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:06:56+00:00 2026-05-27T13:06:56+00:00

This is a function decode the base64. SIGSEGV(sometimes it is SIGABORT) occurs at the

  • 0

This is a function decode the base64. SIGSEGV(sometimes it is SIGABORT) occurs at the line of the malloc called. It almost drive me crazy! Thanks in advance.

static char base64_table[255] = {'\0'};

static void base64_tableinit()
{
   int i,j;
   bzero(base64_table,255);
   for(j=0,i='A';i<='Z';i++) 
       base64_table[i]=j++;
   for(i='a';i<='z';i++)
       base64_table[i]=j++;
   for(i='0';i<='9';i++)
       base64_table[i]=j++;

   base64_table['+']=j++;
   base64_table['/']=j++;
   base64_table['=']=j;
}    
char *decode(const char *cptr,char **rptr)
{
    if(cptr==NULL)
    {
        fprintf (stderr, "The input string is NULL!\n");
        exit(1);
    }

    int len = strlen(cptr);
    if(len%4 != 0)  
    {
        fprintf (stderr, "The input string length is not 4X!\n");
        exit(1);
     }

     base64_tableinit();
     int clen=len/4;

#ifdef DEBUG
    /// printf ("The length of string len = %d\n",len);
    /// printf ("The length of string clen = %d\n",clen);
#endif
    char* res = NULL;
    /// Error: below, SIGSEGV
    if((res=(char *)malloc((len-clen + 1) * sizeof(char)))==NULL)
    {
        fprintf (stderr, "Can't malloc enough space in decode!\n");
        exit(1);
    }

    for(*rptr=res; clen>0; clen--)
    {
        *res = base64_table[(int)*cptr++]<<2 ;              /* Use the 1th char(6) */
        *res++|= base64_table[(int)*cptr]>>4 ;               /* Use the 2th char(2) */ /// Construct the first char 

        *res = base64_table[(int)*cptr++]<<4 ;              /* Use the 2th char(4) */
        *res++ |= base64_table[(int)*cptr]>>2 ;             /* Use the 3th char(4) */  /// Construct the second char 


        *res = base64_table[(int)*cptr++]<<6;               /* Use the 3th char(2) */
        *res++ |= base64_table[(int)*cptr++]&0x3f;          /* Use the 4th char(6) */  /// Construct the third char 

    }
    *(res+len-clen) = '\0';

    return *rptr;
}

The backtrace of gdb.

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb71a2b70 (LWP 5432)]
0xb7d450a6 in _int_malloc () from /lib/libc.so.6
(gdb) bt
0  0xb7d450a6 in _int_malloc () from /lib/libc.so.6
1  0xb7d4746c in malloc () from /lib/libc.so.6
2  0x0804b04c in decode (
cptr=0x806dce8  "PGRpdiBzdHlsZT0ibGluZS1oZWlnaHQ6MS43O2NvbG9yOiMwMDAwMDA7Zm9udC1zaXplOjE0cHg7Zm9udC1mYW1pbHk6YXJpYWwiPuato+W4uCA8YnI+PC9kaXY+PGJyPjxicj48c3BhbiB0aXRsZT0ibmV0ZWFzZWZvb3RlciI+PHNwYW4gaWQ9Im5ldGVhc2VfbWFp"..., rptr=0xb71a1fa8) at base64.c:78
3  0x0804d5af in email_decode (email_old=0xb71a1ffc, email_new=0xb71a1d50) at email_handle.c:421
4  0x0804a9c2 in PacketAnalyze () at packet_analyze.c:800
5  0xb7fa1cf2 in start_thread () from /lib/libpthread.so.0
6  0xb7da584e in clone () from /lib/libc.so.6
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:06:56+00:00Added an answer on May 27, 2026 at 1:06 pm

    This:

    *(res+len-clen) = '\0';
    

    seems wrong, since you’ve already incremented res all through the loop, it should probably just be

    *res = '\0';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode the result of the Python os.wait() function. This returns, according
I am using this class to encode/decode text to base64. It works fine with
This function appears to be a way to access all sorts of system values.
This function declaration gives me errors: ostream& operator<<(ostream& os, hand& obj); The errors are:
This function is written in ActionScirpt. What kind of decryption this is? Is there
This function of mine keeps on failing an autograder, I am trying to figure
Is this function declaration in C#: void foo(string mystring) the same as this one
Imagine this function: void SoundManager::playSource(ALuint sourceID, float offset) { alSourceStop(sourceID); ALint iTotal = 0;
Since this function is implemented in IE8, I wanted to see exactly what I
Example: /** * This function will determine whether or not one string starts with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.