0

我一直在四处寻找答案,但我仍然不清楚我应该做什么。

在所有官方示例中,所有值和索引以及指向它们的指针都是 Word_t 或 PWord_t 类型,据我所知,这只是某种类型的 int。我的困惑是试图理解它如何引用 char* 或缓冲区(char[])或原始字符串(“string”)

这是我尝试过的东西,但输出难以理解(就像您在打印二进制文件时看到的那样)

#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;
} 

输出是:

`(@��
4

1 回答 1

0

我通过我的工具运行了您的代码,它显示代码存在缓冲区溢出问题。这是错误消息:

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 

基本上,&PValue 获取 Pvalue 的地址,而 printf 将其视为 4 字节的 JudySL 数组的地址。如果你改变

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

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

它会为我打印出“P{WHAT”。我不熟悉 Judy hash,所以你需要验证它是否是预期的结果。

于 2017-09-06T07:18:52.207 回答