- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图释放为字符串创建的 malloc 缓冲区,但 free() 给了我一个错误。正如我所看到的,指针的值没有改变,并且两个数组都被分配了。那么应该可以释放他们吗?我想不起来我做错了什么。
这是代码:
/* dump
* this function dumps the entry array to the command line
* */
void dump(PasswordEntry * entries, int numLines) {
int index = 0;
unsigned char *hexSalt = malloc(SALT_HEX_LENGTH+1), *hexHash = malloc(MAX_HASH_LEN+1); /* pointers for salt and hash, because we need them in hex instead of byte */
while (index < numLines) { /* go through every line */
/* gets us the salt in hex */
toHexBinary(hexSalt, entries[index].salt, SALT_HEX_LENGTH);
/* gets us the hash in hex, with length according to set algorithm */
toHexBinary(hexHash, entries[index].hash, (entries[index].algorithm == HASH_ALG_SHA1)?SHA1_HEX_LENGTH:SHA2_HEX_LENGTH);
/* prints one line to command line */
printf("%s: %s = %s (%s/%s)\n", entries[index].username, hexHash, (entries[index].password == NULL)?"???":entries[index].password, (entries[index].algorithm == HASH_ALG_SHA1)?"SHA1":"SHA2", hexSalt);
index++;
}
/* don't need these anymore, we can free them */
free(hexSalt);
free(hexHash);
}
/* takes a string in binary and returns it in hex (properly escaped) */
unsigned char * toHexBinary(unsigned char * to, unsigned char * from, int length) {
unsigned char c = '0';
int second = 0, first = 0;
if (to == NULL) { /* if to is null, we need to allocate it */
to = malloc(length+1);
}
to[length] = '\0';
while (length-- > 0) { /* go trough the string, starting at tthe end */
length--; /* we always need to read two characters */
c = from[length/2];
second = c % 16;
first = (c - second) / 16;
to[length] = toHex(first);
to[length+1] = toHex(second);
}
return to;
}
/* takes a numeric character and returns it's hex representation */
char toHex(int c) {
if (c < 10) return (char)(NUMBER_BEGIN + c); /* if it is under 10, we get the appropiate digit */
else return (char)(UPPER_BEGIN + (c - 10)); /* if it is over 10, we get the appropiate UPPERCASE character */
}
这是 gdb 的输出:
Starting program: /crack -b ./hashes.txt 1 2
Breakpoint 1, dump (entries=0x604700, numLines=9) at crack.c:435
435 unsigned char *hexSalt = malloc(SALT_HEX_LENGTH+1), *hexHash = malloc(MAX_HASH_LEN+1); /* pointers for salt and hash, because we need them in hex instead of byte */
(gdb) next
437 while (index < numLines) { /* go through every line */
(gdb) p hexSalt
$1 = (unsigned char *) 0x604390 ""
(gdb) p hexHash
$2 = (unsigned char *) 0x604510 ""
(gdb) continue
Continuing.
Breakpoint 2, dump (entries=0x604700, numLines=9) at crack.c:449
449 free(hexSalt);
(gdb) p hexSalt
$3 = (unsigned char *) 0x604390 "1234567890FEDCBA0000"
(gdb) p hexHash
$4 = (unsigned char *) 0x604510 "05F770BDD6D78ED930A9B6B9A1F22776F13940B908679308C811978CD570E057"
(gdb) next
450 free(hexHash);
(gdb) next
*** Error in `/crack': free(): invalid next size (fast): 0x0000000000604510 ***
Program received signal SIGABRT, Aborted.
0x00007ffff7602267 in __GI_raise (sig=sig@entry=6)
at ../sysdeps/unix/sysv/linux/raise.c:55
55 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
最佳答案
toHexBinary(hexHash, entries[index].hash, (entries[index].algorithm == HASH_ALG_SHA1)?SHA1_HEX_LENGTH:SHA2_HEX_LENGTH);
您仅为 hexHash
分配 MAX_HASH_LEN+1
字节。但您正在传递 SHA1_HEX_LENGTH
或 SHA2_HEX_LENGTH
。
如果这些值中的任何一个大于 MAX_HASH_LEN
,则会出现问题,因为函数 toHexBinary()
访问 hexHash[MAX_HASH_LEN]
。这可能就是发生的情况。您不能传递大于 MAX_HASH_LEN
的值。
关于c - malloc 对象上的 Free throws 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34465734/
这个问题在这里已经有了答案: How do free and malloc work in C? (8 个答案) 关闭 8 年前。 如果你使用malloc()为4个整数分配内存,它不应该返回第一个整
首先,介绍一下背景知识,这样您就不会认为我在尝试做一些疯狂的事情: 我正在尝试调试由其他人编写的 C 库中的崩溃。崩溃看起来像这样: TheProgram(44365,0x7fff75996310)
我正在 cstdlib malloc() 和 free() 函数之上创建自定义内存分配器(出于性能原因)。分配器位于一个简单的类中,该类存储一些内存缓冲区和其他参数。我想将释放内存的方法命名为 fre
我一直在解决这个练习,我不知道从哪里开始: 语言 B 是上下文无关的;语言 C 是 B 的子集:C 是否是上下文无关的?证明或反驳。 我试过使用闭包属性: C = B - ( (A* - C) ∩ B
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
如果我想获得在 C 中进行 malloc 的指针的所有权,则 docs for the Python cffi package和 this answer假设使用 ffi.gc 和 lib.free 作
#include #include struct node { int value; struct node* next; }; typedef struct node node_
众所周知,Oracle 在 Java 11 中更改了 Java 许可证,要求 JDK 的商业用途需要付费许可证。然而,使用 OpenJDK 仍然是免费的。 我的 PC 上有一个 JDK 11 文件夹,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我是 C 的新手,在 Linux 中使用带有开关 gcc -g -std=c89 -Wall ... 的 gcc4.4.6 进行编程,我在许多函数深处遇到了这个错误我的程序名为 compute: **
在多线程编程中,我们可以找到两个或多个线程/任务之间的数据传输同步的不同术语。 什么时候我们可以说某个算法是: 1)Lock-Free 2)Wait-Free 3)Wait-Freedom 我明白无锁
我正在尝试使用我通过 malloc() 手动分配的数组来运行程序。我在程序末尾释放()这个数组,但我不断收到错误消息 *** glibc detector *** ./test: double fre
我将 libxml2 与 libxslt 一起用于 C++ 程序的 XML 处理。为了使用 XSL 转换 XML 文档,我使用了以下函数(删除了错误处理): xmlDocPtr transformXm
new/delete 关键字使用免费商店 malloc/free 关键字是使用堆 我看到某处写着new 使用malloc。怎么会这样?它们不在内存段中使用? 其次,我看到某处写道我们不能在new 之后
我有这个简单的代码,我想在 tutorialspoint.com 上运行 #include using namespace std; class Vehicle { string vehic
我需要创建一个函数来删除 c 中链表的前 n 个节点,并返回删除的节点数。如果列表小于 n,它应该变为空。 另外,我不能使用递归。 使用现在的代码,它可以工作,但我没有释放“已删除”节点的内存。如果我
我需要调试这段代码的帮助。我知道问题出在 malloc 和 free 中,但找不到确切的位置、原因和解决方法。请不要回答:“使用 gdb”,仅此而已。我会使用 gdb 来调试它,但我仍然不太了解它并且
这个问题在这里已经有了答案: Unable to free const pointers in C (12 个答案) 关闭 8 年前。 将 C++11 代码连接到某些 C 回调,我必须传递 cons
这是出于好奇,我试图找到我对之前问题的疑问的答案,但他们似乎没有答案。所以在这里问,我只是写了一个代码,我试图将内存分配给一个 int 指针(以填充一个数组)并将 int 值扫描到它。完成数组后,我想
我有两个免费的单子(monad),用于不同上下文中的不同操作。但是,如果特定操作位于上下文中,则一个(主要)DSL 需要包含另一个(action)DSL: import Control.Monad.F
我是一名优秀的程序员,十分优秀!