围绕 MD5 代码的分段错误

Segmentation fault around MD5 code

当我运行宁此 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

对于这个声明:

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() 中的 return 分配给变量 len

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

Return值printf:

Upon successful return, it returns the number of characters printed (excluding the null byte used to end output to strings).

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

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