【问题标题】:Segmentation fault around MD5 codeMD5 代码周围的分段错误
【发布时间】:2015-06-26 11:38:08
【问题描述】:

当我运行这个 md5 代码时,它在运行时最多需要 64 个字符长度的输入。每当我给出超过 64 个字符时,它就会显示

Inconsistency detected by ld.so: dl-fini.c: 205: _dl_fini: Assertion ns != 0 || i == nloaded failed!

我需要散列近 10kb 的输入(仅字符串)。我需要更改头文件中的任何内容吗?谁能告诉我解决办法吗?

md5.h

#ifndef HEADER_MD5_H
#define HEADER_MD5_H

#include <openssl/e_os2.h>
#include <stddef.h>

#ifdef  __cplusplus
extern "C" {
#endif

#ifdef OPENSSL_NO_MD5
#error MD5 is disabled.
#endif

/*
  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  * ! MD5_LONG has to be at least 32 bits wide. If it's wider, then !
  * ! MD5_LONG_LOG2 has to be defined along.                        !
  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/

#if defined(__LP64__)
#define MD5_LONG unsigned long
#elif defined(OPENSSL_SYS_CRAY) || defined(__ILP64__)
#define MD5_LONG unsigned long
#define MD5_LONG_LOG2 3  
/*
  * _CRAY note. I could declare short, but I have no idea what impact
  * does it have on performance on none-T3E machines. I could declare
  * int, but at least on C90 sizeof(int) can be chosen at compile time.
  *  So I've chosen long...
  *                                      <appro@fy.chalmers.se>
 */
#else
#define MD5_LONG unsigned long   
#endif

#define MD5_CBLOCK      64
#define MD5_LBLOCK      (MD5_CBLOCK/2)
#define MD5_DIGEST_LENGTH 16

typedef struct MD5state_st
    {
    MD5_LONG A,B,C,D;
    MD5_LONG Nl,Nh;
    MD5_LONG data[MD5_LBLOCK];
    unsigned int num;
    } MD5_CTX;

#ifdef OPENSSL_FIPS
int private_MD5_Init(MD5_CTX *c);
#endif
int MD5_Init(MD5_CTX *c);
int MD5_Update(MD5_CTX *c, const void *data, size_t len);
int MD5_Final(unsigned char *md, MD5_CTX *c);
unsigned char *MD5(const unsigned char *d, size_t n, unsigned char *md);
void MD5_Transform(MD5_CTX *c, const unsigned char *b);
#ifdef  __cplusplus
}
#endif

#endif

md5.c

  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include "md5.h"

 char *pt(char *, int );
 int main(int argc, char **argv) 
 {
    char *in;
    char *out;
    printf("ENter the string\n");
    scanf("%[^\n]s",in);
    size_t len;    //unsigned long len;  size_t len; 
    len = printf("len is %d\n",strlen(in));
    out = pt(in, len);
    printf("MD5 is\t: %s\n", out);
    free(out);
    //return 0; 
 }


 char *pt(char *str, int length) 
 {
   int n;
   MD5_CTX c;
   unsigned char digest[16];
   char *output = (char*)malloc(33);

   MD5_Init(&c);

   MD5_Update(&c, str, length);

   MD5_Final(digest, &c);

   for (n = 0; n < 16; ++n)
   {
     sprintf(&output[n*2], "%02x", (unsigned int)digest[n]);
   }

   return output;
}

【问题讨论】:

  • char *in; char *out; printf("ENter the string\n"); scanf("%[^\n]s",in); *in 和 *out 未初始化(并且它们不指向任何地方)

标签: c openssl runtime-error md5


【解决方案1】:

问题 1

对于这个声明:

scanf("%[^\n]s",in);

当我使用 -Wall 标志编译它时,我收到警告:

warning: 'in' is used uninitialized in this function [-Wuninitialized] scanf("%[^\n]s",in); ^

如您所见,in 并未指向您内存中的任何位置,因此您首先需要使用数组或malloc() 分配一些内存:

char in[500]; //or a higher value
char *out;
printf("Enter the string\n");
scanf("%499[^\n]s", in);
printf("\nin = .%s.\n", in);

char *in;
char *out;
in = malloc(500); //or a higher value
printf("Enter the string\n");
scanf("%499[^\n]s", in);
printf("\nin = .%s.\n", in);

可能的问题2

您正在将来自printf() 的返回分配给变量len

len = printf("len is %d\n",strlen(in));

返回值printf

返回成功后,返回打印的字符数(不包括用于结束输出到字符串的空字节)。

假设您希望变量 len 包含字符串 in 的长度而不是 printf("len is %d\n",strlen(in)) 打印的字符数,您可能希望首先分配来自 strlen() 的返回值:

len = strlen(in);
printf("len is %d\n", len);

【讨论】:

  • 如果我服用 --> int len; len =strlen(in); printf("长度 \n",len);像这样,问题无法解决,问题是输入不一致。它不接受超过 64 个字符的输入。
  • "%[^\n]s" 是一个不正确的格式字符串,它应该是"%499[^\n]"
  • @MattMcNabb 谢谢,我的测试通过了,所以我忽略了它。你有参考吗?
  • 但是为什么它只占用最大 64 字符长度的输入......,你能告诉我答案吗!!!
猜你喜欢
  • 2011-09-20
  • 2019-06-26
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多