MD5代码周围的分段错误

时间:2015-06-26 11:38:09

标签: c openssl runtime-error md5

当我运行这个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;
}

1 个答案:

答案 0 :(得分:2)

问题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))打印的字符数,您可能希望从{{3}分配返回值首先:

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