0

我正在使用以下函数循环访问几个打开的 CDB 哈希表。有时,给定键的值与附加字符(特别是 CTRL-P(DLE 字符/0x16/0o020))一起返回。

我已经使用几个不同的实用程序检查了 cdb 键/值对,但它们都没有显示附加到值的任何附加字符。

如果我使用 cdb_read() 或 cdb_getdata() (下面的注释掉的代码),我会得到这个字符。

如果我不得不猜测,我会说我为从 cdb 函数获取结果而创建的缓冲区做错了。

非常感谢任何建议或帮助。

char* HashReducer::getValueFromDb(const string &id, vector <struct cdb *> &myHashFiles)
{

  unsigned char hex_value[BUFSIZ];
  size_t hex_len;

  //construct a real hex (not ascii-hex) value to use for database lookups
  atoh(id,hex_value,&hex_len);

  char *value = NULL;
  vector <struct cdb *>::iterator my_iter = myHashFiles.begin();
  vector <struct cdb *>::iterator my_end = myHashFiles.end();


  try
  {
    //while there are more databases to search and we have not found a match
    for(; my_iter != my_end && !value ; my_iter++)
    {
      //cerr << "\n looking for this MD5:" << id << " hex(" << hex_value << ") \n";
      if (cdb_find(*my_iter, hex_value, hex_len)){
          //cerr << "\n\nI found the key " << id << " and it is " << cdb_datalen(*my_iter) << " long\n\n";
          value = (char *)malloc(cdb_datalen(*my_iter));
          cdb_read(*my_iter,value,cdb_datalen(*my_iter),cdb_datapos(*my_iter));
          //value = (char *)cdb_getdata(*my_iter);
          //cerr << "\n\nThe value is:" << value << " len is:" << strlen(value)<< "\n\n";
        };

    }
  }
  catch (...){}
  return value;
}
4

1 回答 1

0

首先,我对 CDB 不熟悉,而且我不相信您在此处包含有关您的软件环境的足够详细信息。

但是假设它就像我使用过的其他数据库一样......

这些值可能不必以 NUL 结尾。这意味着转换为 char* 并打印它是行不通的。您应该自己添加一个 0 字节。

所以 malloc cdb_datalen + 1 并将最后一个字符设置为0。然后打印它。

更好的是,使用calloc它会分配已经设置为零的内存。

于 2010-04-13T00:18:41.060 回答