C: How to insert and read a string to/from a Judy Hash

193 views Asked by At

I've been searching around for a while to get an answer but I am still very unclear on what I am supposed to do.

In all the official examples all the Values and indexes and pointers to them are of the type Word_t or PWord_t, which as far as I can tell is just an int of some kind. My confusion is trying to understand how this can refer to a char* or buffer (char[]) or raw string ("string")

Here's something I've tried, but the output is unintelligible (like what you see when you print a binary)

#include <stdio.h>
#include <Judy.h>


int main()
{

        Pvoid_t   PJArray = (PWord_t)NULL;  // Judy array.
        PWord_t   PValue;                   // Judy array element.
        Word_t    Bytes;                    // size of JudySL array.

        JSLI(PValue, PJArray, "WHAT");
        *PValue = "HELLO...";

        JSLG(PValue, PJArray, "WHAT");
        printf("%s\n", &PValue);

        return 0;
} 

output is:

`(@��
1

There are 1 answers

0
stensal On

I ran your code thru my tool and it shows the code has a buffer overrun issue. This is the error msg:

DTS_MSG: Stensal DTS detected a runtime program error. Abort execution!
DTS_MSG: Reading 1 bytes at 0xbfffdba0 violates type safety.
DTS_MSG: Diagnostic information:

- The object to-be-read (start:0xbfffdb9c, size:4 bytes) is allocated at
-     file:/tmp/a.c::9, 19 
-  0xbfffdb9c               0xbfffdb9f
-  +------------------------+
-  | the object  to-be-read |......
-  +------------------------+
-                            ^~~~~~~~~~
-        the read starts at 0xbfffdba0 that is right after the object end.
- Stack trace (most recent call first):
-[1]  file:/src/string/memchr.c::25, 9 
-[2]  file:/src/stdio/vfprintf.c::602, 8 
-[3]  file:/src/stdio/vfprintf.c::678, 8 
-[4]  file:/src/stdio/printf.c::9, 8 
-[5]  file:/tmp/a.c::16, 9 
-[6]  file:/src/env/__libc_start_main.c::149, 11 

Basically, &PValue takes the address of Pvalue and printf treats it as the address of a JudySL array of 4 bytes. If you change

 printf("%s\n", &PValue);

to

 printf("%s\n", PValue);

It will print out "P{WHAT" for me. I'm not familiar with Judy hash, so you need to verify if it's the intended result.