- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个函数,用于搜索模式的所有出现并返回文件中与该模式匹配的偏移量数组。我想使用 realloc 动态增长返回的数组,但出现 SYSMALLOC 断言错误。有时,如果我使用不同的搜索模式,我可能会收到无效的下一个尺寸错误。我的机器上还没有 valgrind (需要使用调试标志重建 glibc)。这似乎是一个简单的问题,我只触摸这个指针两次——一次声明它并将其设置为 NULL,再次重新分配以增长它。我知道 sizeof(char) 是 1,在我的 realloc 语句中没有用。
这是有问题的函数的代码。
unsigned long long* searchBytes(FILE* fp, char* byteString, char* searchType, unsigned long long fileSize)
{
if (fp == NULL)
return NULL;
unsigned long long* foundBytes = NULL;
long numBytes = 0;
// make some memory for the array of found bytes
if (strcmp(searchType, "ascii") == 0)
{
numBytes = strlen(byteString);
//foundBytes = realloc(NULL, numBytes * sizeof(char));
}
else
{
// TODO strip the spaces from the string and handle hex searches
printf("hex-search not implemented yet.\n");
return NULL;
}
// loop over all the bytes in the file looking for this ascii pattern
unsigned long long currentOffset = 0;
unsigned long long origOffset = 0;
unsigned long long m = 0;
foundWords = 0;
char* possibleWord = malloc(numBytes * sizeof(char));
do
{
fseek(fp, currentOffset, SEEK_SET);
unsigned long long i;
int n = 0;
int failed = 0;
origOffset = currentOffset;
for(i=currentOffset; i<currentOffset+numBytes; i++)
{
possibleWord[n] = fgetc(fp);
n++;
}
//printf("possibleWord: %s\n", possibleWord);
// is this our word? use strstr just in case
char* found = strstr((const char*) byteString, (const char*) possibleWord);
if (found)
{
foundWords++;
// make a bigger spot for it
printf("allocating %ld bytes to add word %d to list...\n", (numBytes*foundWords) * sizeof(char), foundWords);
unsigned long long* p = realloc(foundBytes, (numBytes*foundWords) * sizeof(char));
if (p)
{
foundBytes = p;
for (i = origOffset; i<origOffset+numBytes; i++)
{
foundBytes[m] = i;
//printf("added offset %llu to foundBytes[%llu]\n", i, m);
m++;
}
}
else
{
return NULL;
}
}
else
{
failed = 1;
}
if (failed == 0)
{
currentOffset += numBytes;
//printf("Yay! moving forward %ld bytes.\n", numBytes);
}
else
{
currentOffset++;
}
}
while (currentOffset < fileSize);
if (foundWords > 0)
{
//printf("returning foundBytes!\n");
//unsigned long long z;
//for (z=0; z<foundWords*numBytes; z++)
// printf("%llu\n", foundBytes[z]);
//printf("...\n");
return foundBytes;
}
//printf("returning NULL\n");
return NULL;
}
<小时/>
当使用“root”作为搜索模式在/etc/passwd 上运行时:
allocating 4 bytes to add word 1 to list...
allocating 8 bytes to add word 2 to list...
*** glibc detected *** ./chex3: realloc(): invalid next size: 0x0000000001a59270 ***
或者在/etc/passwd 上使用“daemon”作为搜索模式:
allocating 6 bytes to add word 1 to list...
allocating 12 bytes to add word 2 to list...
chex3: malloc.c:2451: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
有人可以看看这个并看看它看起来是否正常吗?谢谢!我是一个正在努力学习的菜鸟:)
最佳答案
解决方案是使用foundBytes 对 p 进行 decalure,然后将 realloc 行更改为:
p = realloc(foundBytes, (numBytes*foundWords) * sizeof(*p));
我必须承认我真的不明白为什么这是必要的,看起来我这样分配的已经绰绰有余了。 (每次添加 24 个字节,而不是 3 个字符搜索模式的 3 个字节)
关于C-SYSMALLOC : Assertion failed (realloc),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13927593/
测试返回类型为 bool 的方法时。 你应该: expected = true; Assert.AreEqual(expected, actual); 或 Assert.IsTrue(actual);
我最近在编写新的 NUnit 测试时尝试使用 Assert.Equals() 方法。执行此方法时会抛出一个 AssertionException ,说明Assert.Equals 不应该用于断言。 乍
在 Chai 断言库中,当我们已经有了“assert.deepEqual()”时,“assert.equal()”有什么用"和 "assert.strictEqual()"用于严格和深度相等断言?还提
有没有办法断言 puppet 中的变量(或更具体地说,事实)具有特定值,如果没有则中止安装? 对于背景,情况如下: 在大多数情况下,我可以引用主机名,但有时我需要使用 IP 地址。例如,我们的日志收集
喜欢什么: Assert.That(obj.Foo, Is.EqualTo(true)) 或 Assert.True(obj.Foo) 对我来说,这两个断言是等价的,那么应该首选哪个? 最佳答案 在这
如何在 xUnit 中找到多个断言或软断言?我发现 Nunit 有以下能力,试图在 xUnit 中找到类似的选项。 Assert.Multiple(() => { Assert.AreEqua
有什么区别: Assert.Equals和 Assert.AreEqual Assert.NotNull和 Assert.IsNotNull ... ? 最佳答案 Assert.Equals 是一个对
我想写一个像这样工作的断言函数: //the following expression outputs "assertion failed" to std::err and then terminat
有人可以指出差异吗? 以上确实是我的问题,但是如果您也可以与他们分享您的经验以及您为什么使用其中一个。 最佳答案 它们只是两个不同的库,因此只需查看功能,尤其是报告功能,然后选择即可。 因为我是 的作
我无法找到断言 1 失败但断言 2 通过的原因: var a = Test.test1; var b = Test.test1; a.Should().BeSameAs(b); //1 Assert.
我正在为每个步骤使用 NUnit 断言运行自动化 BDD 步骤,即 Then And 我的 UI 测试。 NUnit 断言仅限于每个方法。这意味着如果方法中的断言失败,则不会运行其他步骤。 我正在考虑
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我只是在寻找一些示例,说明何时适合使用 Assert.Catch 或 Assert.Throws 断言单元测试中抛出的任何异常。我知道我也可以使用 ExpectedException,但我特别想知道“
Assert.AreEqual 和 Assert.AreSame 有什么区别? 最佳答案 这意味着 AreSame() 检查它们是否是完全相同的对象 - 如果引用指示内存中的相同对象。 AreEqua
在C#中,有什么区别 Assert.AreNotEqual 和 Assert.AreNotSame 最佳答案 这里给出的几乎所有答案都是正确的,但可能值得举个例子: public static str
我曾经在 NUnit 中使用过它们,它们非常有用。知道如何做类似的事情吗? 编辑,代码示例: bool condition = false;//would be nice not to have th
关于Arrange-Act-Assert的经典测试模式,我经常发现自己在 Act 之前添加了反断言。这样我就知道传递的断言确实是作为操作的结果传递的。 我认为它类似于红绿重构中的红色,只有当我在测试过
每当我创建断言时,Eclipse 都会建议我从这两个包之一导入它。 例如,当我尝试使用 assertArrayEquals() 比较数组时Eclipse 建议从其中之一导入它 org.junit.As
每当我创建断言时,Eclipse 都会建议我从这两个包之一导入它。 例如,当我尝试使用 assertArrayEquals() 比较数组时Eclipse 建议从其中之一导入它 org.junit.As
我是一名优秀的程序员,十分优秀!