c - Segmentation fault around MD5 code -


while running md5 code, taking maximum 64 characters length of input @ run time. whenever giving more 64 characters, showing

inconsistency detected ld.so: dl-fini.c: 205: _dl_fini: assertion ns != 0 || == nloaded failed!

i need hash 10kb of input (only string). need change in header file? can tell me solution please?

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 disabled. #endif  /*   * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   * ! md5_long has @ least 32 bits wide. if it's wider, !   * ! md5_long_log2 has 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. declare short, have no idea impact   * have on performance on none-t3e machines. declare   * int, @ least on c90 sizeof(int) can chosen @ compile time.   *  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 string\n");     scanf("%[^\n]s",in);     size_t len;    //unsigned long len;  size_t len;      len = printf("len %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);     (n = 0; n < 16; ++n)    {      sprintf(&output[n*2], "%02x", (unsigned int)digest[n]);    }     return output; } 

problem 1

for statement:

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

when compile using -wall flag, warning:

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

as see, in not pointing location in memory, first need allocate memory either array or malloc():

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

or

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

possible problem 2

you assigning return printf() variable len.

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

return value printf:

upon successful return, returns number of characters printed (excluding null byte used end output strings).

assuming want variable len contain length of string in , not number of characters printed printf("len %d\n",strlen(in)), might want assign return strlen() first:

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

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -