c - lhash 在 C - Linux 中的 lh_insert 上崩溃?

标签 c linux openssl

我正在尝试编译一个示例程序以使用lhash。我看不到关于 lhash 的好的教程。所以,我理解 lhash 的唯一方法是使用 lhash linux 手册页。这是我正在尝试制作的示例。但是,我在执行 lh_insert 时发生崩溃。我不知道为什么会发生这种情况。

/** In order to compile this program do the following **/

/**  gcc lhastEx.c -lcrypto -o lhastEx.out **/

/** Install the openssl dev library on ubuntu by -- sudo apt-get install libssl-dev **/
/*** This is needed for library hash -- basically open ssl ones **/
#include <openssl/lhash.h>

/** Hash table -- just like maps in C++ i.e. QMAP -- it needs a key and the value **/

/*I have got a prints to check the flow */
#define __DBG (1)

static void dbgMsg(const char *msg)
{
#if __DBG
   printf("%s",msg);
#endif 
}


static int cmpFunc(const void *src, const void *dest)
{
    dbgMsg("cmpFunc called..\r\n");

    const int *obj1 = src;
    const int *obj2 = dest;

    return memcmp(obj1, obj2, sizeof(int));

}

static unsigned long keyHash(const void *entry)
{
    unsigned long int hash = 0;
    const int *val = entry;
    dbgMsg("keyHash method invoked\r\n");

    hash |= *(val);

    return hash;
}

int main(int argc, char *argv[])
{
   int *hashKey2 = malloc(sizeof(int));

   int *hashKey3 = malloc(sizeof(int));

   int *hashKey1 = malloc(sizeof(int));

   *hashKey1 = 10;
   *hashKey2 = 20;
   *hashKey3 = 30;

   /* we can make a function to generate this key unique **/
   /** Ideally, this 1 should be a unique hash value **/

    /************** Created the hash table now -- I see this as equivalent to the map in C ++ or QtMap **/   
    LHASH_OF(int) *hashtable = lh_new(keyHash, cmpFunc);
   /*** add a new entry now **/


   lh_insert(hashtable, hashKey2);


  return 0;

}

最佳答案

LHASH API 的使用方式(有点)类似于 STACK API 。此外,还有 OpenSSL 源代码可以帮助您了解它。例如,请参阅使用 ERR_STRING_DATA 哈希表的 crypto/err/err.c,并参阅 lhash.hsafestack。 h 用于宏定义。

最安全的方法是为表定义强类型函数。在这种情况下,一个关键方面是哈希表中的元素必须是结构类型。请参阅下面的代码,了解如何通过使用具有单个 int 字段的结构来处理整数哈希表。

#include <openssl/lhash.h>
#include <string.h>

#define __DBG (1)

static void dbgMsg(const char *msg)
{
#if __DBG
    printf("%s", msg);
#endif 
}

typedef struct int_value_st {
    int value;
} INT_VALUE;

static int int_value_cmp(const INT_VALUE *a, const INT_VALUE *b)
{
    dbgMsg("cmpFunc called..\r\n");
    return a->value - b->value;
}
static IMPLEMENT_LHASH_COMP_FN(int_value, INT_VALUE);

static unsigned long int_value_hash(const INT_VALUE *entry)
{
    unsigned long int hash = 0;
    dbgMsg("keyHash method invoked\r\n");

    hash |= entry->value;

    return hash;
}
static IMPLEMENT_LHASH_HASH_FN(int_value, INT_VALUE);

/* See stack/safestack.h for a complete list of the possible #defines */
#define lh_INT_VALUE_new() LHM_lh_new(INT_VALUE,int_value)
#define lh_INT_VALUE_insert(lh,inst) LHM_lh_insert(INT_VALUE,lh,inst)
#define lh_INT_VALUE_retrieve(lh,inst) LHM_lh_retrieve(INT_VALUE,lh,inst)
#define lh_INT_VALUE_delete(lh,inst) LHM_lh_delete(INT_VALUE,lh,inst)
#define lh_INT_VALUE_free(lh) LHM_lh_free(INT_VALUE,lh)

int LHashTest(void)
{
    DECLARE_LHASH_OF(INT_VALUE);

    INT_VALUE *hashKey1 = OPENSSL_malloc(sizeof(*hashKey1));
    INT_VALUE *hashKey2 = OPENSSL_malloc(sizeof(*hashKey2));
    INT_VALUE *hashKey3 = OPENSSL_malloc(sizeof(*hashKey3));
    INT_VALUE *hashKeyFound = NULL;

    hashKey1->value = 10;
    hashKey2->value = 20;
    hashKey3->value = 30;

    LHASH_OF(INT_VALUE) *hashtable = NULL;
    hashtable = lh_INT_VALUE_new();
    lh_INT_VALUE_insert(hashtable, hashKey1);
    lh_INT_VALUE_insert(hashtable, hashKey2);
    lh_INT_VALUE_insert(hashtable, hashKey3);

    /* Should find result */
    hashKeyFound = lh_INT_VALUE_retrieve(hashtable, hashKey2);
    lh_INT_VALUE_delete(hashtable, hashKeyFound);
    /* Should not find result */
    hashKeyFound = lh_INT_VALUE_retrieve(hashtable, hashKey2);

    /* OPENSSL_free()s all elements */
    lh_INT_VALUE_free(hashtable);

    return 1;
}

顺便说一句,您似乎忽略了一些编译器警告...这通常不是一件好事。

关于c - lhash 在 C - Linux 中的 lh_insert 上崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33684774/

相关文章:

c - mmap 内存由其他内存支持吗?

linux - smb for Linux 只适合环境吗?

linux - 复制在 2 个 SVN 版本之间更改的文件和文件夹

openssl sha256 差异

c - 来自 ANSI C K&R 的位运算符

c - 32位单元C编程语言的范围

c - 这个节目敲响了警钟!

php - 使用 php (Raspberry Pi) 设置 cronjob

ubuntu - 除非明确指定 CAfile,否则 OpenSSL 无法获取本地颁发者证书

c++ - Eclipse CDT - 链接 libcrypto.so 用于交叉编译