【问题标题】:Seg fault when trying to generate base64 encode value - C尝试生成 base64 编码值时出现 Seg 错误 - C
【发布时间】:2013-01-15 20:43:47
【问题描述】:

我正在尝试通过使用提到的实现here 来获取字符串值“简单”的 Base 64 编码值

#include<openssl/sha.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdint.h>
#include<stdlib.h>


static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                                'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                                'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                                'w', 'x', 'y', 'z', '0', '1', '2', '3',
                                '4', '5', '6', '7', '8', '9', '+', '/'};

static int mod_table[] = {0,2,1};

char *base64_encode(const unsigned char *data,
                    size_t input_length,
                    size_t *output_length)
{
        printf("-- Begins -- ");
        *output_length = (size_t) (4.0 * ceil((double) input_length / 3.0));
        char *encoded_data = malloc(*output_length);

        for (int i = 0, j = 0; i < input_length;) {

        uint32_t octet_a = i < input_length ? data[i++] : 0;
        uint32_t octet_b = i < input_length ? data[i++] : 0;
        uint32_t octet_c = i < input_length ? data[i++] : 0;

        uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

        encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
        }

    for (int i = 0; i < mod_table[input_length % 3]; i++)
        encoded_data[*output_length - 1 - i] = '=';
        printf(" -- Ends -- ");

    return encoded_data;


   }

int main(int argc, char **argv)
{
        unsigned char ibuf[] = "simple";
        size_t *outLen;
        size_t *inLen = (size_t) strlen(ibuf);
        char *encodeVal = base64_encode(ibuf, inLen, outLen);
        return(0);
}

当我尝试使用 gdb 进行调试时,我得到的只是:

Program received signal SIGSEGV, Segmentation fault.
0x000000000040073f in base64_encode (data=0x7fff65298a90 "simple", input_length=6, output_length=0x400a50) at openSha.c:25
25              *output_length = (size_t) (4.0 * ceil((double) input_length / 3.0));

这里的错误是什么?我很久没有用 C 编码了,所以如果我在这里遗漏了一些明显的东西,我很抱歉。

【问题讨论】:

  • 看看这个:size_t *outLen;base64_encode(ibuf, inLen, outLen);:你试图取消引用指向任何东西的指针!与inlen 相同

标签: c debugging segmentation-fault


【解决方案1】:
 size_t *inLen = (size_t) strlen(ibuf);

这看起来不对。

inLen 是一个指向 size_t 的指针,但您为其分配了一个 size_t 值。

还有这个电话:

base64_encode(ibuf, inLen, outLen);

base64_encode 期望 size_t 作为其第二个参数,但您传递的是 size_t *

最后outLen 没有被初始化,但是你将它的值传递给base64_encode

【讨论】:

    【解决方案2】:
    size_t *outLen;
    

    您将未初始化的指针传递给encodeVal,然后写入它指向的位置:

    *output_length = (size_t) (4.0 * ceil((double) input_length / 3.0));
    

    您应该传递size_t 变量的地址,或者至少是一个指向已分配内存的初始化指针。

    还有,

    size_t *inLen = (size_t) strlen(ibuf);
    

    很可能是错的,应该是size_t inLen = strlen(ibuf);

    可能,而不是

    char *encoded_data = malloc(*output_length);
    

    您应该再 malloc 一个,并以 0 结尾编码的字符串(但这取决于用途)。

    【讨论】:

      【解决方案3】:

      此指针未初始化:

      size_t *outLen;

      在第 25 行你想取消引用它。将您的代码更改为:

      size_t outLen;
      base64_encode(ibuf, inLen, &outLen);
      

      【讨论】:

      • 对于某些随机值 ;)
      【解决方案4】:

      我刚刚使用了这段代码。

      这是工作:

      #include <stdint.h>
      #include <stdlib.h>
      
      static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                      'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                                      'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                                      'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                                      'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                                      'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                                      'w', 'x', 'y', 'z', '0', '1', '2', '3',
                                      '4', '5', '6', '7', '8', '9', '+', '/'};
      static char *decoding_table = NULL;
      static int mod_table[] = {0, 2, 1};
      
      void build_decoding_table();
      
      char *base64_encode(const unsigned char *data,
                          size_t input_length,
                          size_t *output_length) {
          int i, j;
          *output_length = 4 *((input_length + 2)/ 3);
      
          char *encoded_data = calloc(*output_length, 1);
          if (!encoded_data) return NULL;
      
          for (i = 0, j = 0; i < input_length;) {
      
              uint32_t octet_a = i < input_length ? data[i++] : 0;
              uint32_t octet_b = i < input_length ? data[i++] : 0;
              uint32_t octet_c = i < input_length ? data[i++] : 0;
      
              uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
      
              encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
              encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
              encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
              encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
          }
      
          for (i = 0; i < mod_table[input_length % 3]; i++)
              encoded_data[*output_length - 1 - i] = '=';
      
          return encoded_data;
      }
      
      
      unsigned char *base64_decode(const char *data,
                          size_t input_length,
                          size_t *output_length) {
          int i, j;
          if (decoding_table == NULL) build_decoding_table();
      
          if (input_length % 4 != 0) return NULL;
      
          *output_length = input_length / 4 * 3;
          if (data[input_length - 1] == '=') (*output_length)--;
          if (data[input_length - 2] == '=') (*output_length)--;
      
          unsigned char *decoded_data = calloc(*output_length, 1);
          if (!decoded_data) return NULL;
      
          for (i = 0, j = 0; i < input_length;) {
      
              uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
              uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
              uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
              uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
      
              uint32_t triple = (sextet_a << 3 * 6)
                              + (sextet_b << 2 * 6)
                              + (sextet_c << 1 * 6)
                              + (sextet_d << 0 * 6);
      
              if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
              if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
              if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
          }
      
          return decoded_data;
      }
      
      
      void build_decoding_table() {
          int i;
          decoding_table = malloc(256);
      
          for (i = 0; i < 0x40; i++)
              decoding_table[encoding_table[i]] = i;
      }
      
      
      void base64_cleanup() {
          free(decoding_table);
      }
      
      
      
      
      #include <string.h>
      #include <stdio.h>
      
      int main(int argc, char **argv){
          if(argc != 2) return -1;
          size_t len = strlen(argv[1]), olen;
          printf("len: %zd\n", 4 *((len + 2)/ 3));
          printf("argument %s coded: %s\n", argv[1], base64_encode(argv[1], len, &olen));
          base64_cleanup();
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2015-08-24
        • 1970-01-01
        • 2021-12-26
        • 1970-01-01
        • 2016-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-23
        相关资源
        最近更新 更多