C++使用 cdb_read 在某些读取中返回额外的字符
我正在使用以下函数循环访问几个打开的 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;
}
I am using the following function to loop through a couple of open CDB hash tables. Sometimes the value for a given key is returned along with an additional character (specifically a CTRL-P (a DLE character/0x16/0o020)).
I have checked the cdb key/value pairs with a couple of different utilities and none of them show any additional characters appended to the values.
I get the character if I use cdb_read() or cdb_getdata() (the commented out code below).
If I had to guess I would say I am doing something wrong with the buffer I create to get the result from the cdb functions.
Any advice or assistance is greatly appreciated.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我不熟悉 CDB,并且我不相信您在这里包含有关您的软件环境的足够详细信息。
但假设它就像我使用过的其他数据库库一样......
这些值可能不必以 NUL 结尾。这意味着转换为 char* 并打印它将不起作用。你应该自己添加一个0字节。
所以 malloc cdb_datalen + 1 并将最后一个字符设置为 0。然后打印它。
更好的是,使用 calloc 它会分配已经设置为零的内存。
First, I am not familiar with CDB and I don't believe you include enough details about your software environment here.
But assuming it is like other database libraries I've used...
The values probably don't have to be NUL-terminated. That means that casting to char* and printing it will not work. You should add a 0 byte yourself.
So malloc cdb_datalen + 1 and set the last character to 0. Then print it.
Better yet, use
calloc
and it will allocate memory already set to zero.