使用 memcpy 连接字节块中的字节时出错

发布于 2024-09-08 01:31:54 字数 1040 浏览 3 评论 0原文

有时,以下代码可以工作,这可能意味着概念很好,但执行很差。由于崩溃取决于位掉落的位置,这意味着我正在一路走来。我有兴趣找到一种优雅的方法来用 buffer 中的 <=4096 字节填充 bufferdata,但不可否认,这不是它。

编辑:我收到的错误是对 bufferdata 的非法访问

unsigned char        buffer[4096] = {0};
char *bufferdata;

bufferdata = (char*)malloc(4096 * sizeof(*bufferdata));
if (! bufferdata)
    return false;

while( ... )
{

    // int nextBlock( voidp _buffer, unsigned _length );
    read=nextBlock( buffer, 4096);

    if( read > 0 )
    {
        memcpy(bufferdata+bufferdatawrite,buffer,read);

        if(read == 4096) {

            // let's go for another chunk
            bufferdata = (char*)realloc(bufferdata, ( bufferdatawrite + ( 4096 * sizeof(*bufferdata)) ) );
            if (! bufferdata) {
                printf("failed to realloc\n");
                return false;
            }

        }

    }
    else if( read<0 )
    {
        printf("error.\n");
        break;
    }
    else {
        printf("done.\n");
        break;
    }
}


free(bufferdata);

On occasion, the following code works, which probably means good concept, but poor execution. Since this crashes depending on where the bits fell, this means I am butchering a step along the way. I am interested in finding an elegant way to fill bufferdata with <=4096 bytes from buffer, but admittedly, this is not it.

EDIT: the error I receive is illegal access on bufferdata

unsigned char        buffer[4096] = {0};
char *bufferdata;

bufferdata = (char*)malloc(4096 * sizeof(*bufferdata));
if (! bufferdata)
    return false;

while( ... )
{

    // int nextBlock( voidp _buffer, unsigned _length );
    read=nextBlock( buffer, 4096);

    if( read > 0 )
    {
        memcpy(bufferdata+bufferdatawrite,buffer,read);

        if(read == 4096) {

            // let's go for another chunk
            bufferdata = (char*)realloc(bufferdata, ( bufferdatawrite + ( 4096 * sizeof(*bufferdata)) ) );
            if (! bufferdata) {
                printf("failed to realloc\n");
                return false;
            }

        }

    }
    else if( read<0 )
    {
        printf("error.\n");
        break;
    }
    else {
        printf("done.\n");
        break;
    }
}


free(bufferdata);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笑百媚生 2024-09-15 01:31:54

很难判断错误在哪里,到处都缺少一些代码。

if(read == 4096) { 看起来像是罪魁祸首,如果 nextBlock 在一次迭代中返回 4000,在下一次迭代中返回 97,该怎么办?现在您需要存储 4097 字节,但您无需重新分配缓冲区来容纳它。

您需要累积字节,并在通过 4096 边界时重新分配。
像这样的东西:

#define CHUNK_SIZE 4096
int total_read = 0;
int buffer_size = CHUNK_SIZE ;
char *bufferdata = malloc(CHUNK_SIZE );
char buffer[CHUNK_SIZE];
while( ... )
{

    // int nextBlock( voidp _buffer, unsigned _length );
    read=nextBlock( buffer, CHUNK_SIZE );

    if( read > 0 )
    {
        total_read += read;
        if(buffer_size < total_read) {
           // let's go for another chunk
            char *tmp_buf;
            tmp_buf= (char*)realloc(bufferdata, buffer_size + CHUNK_SIZE );
            if (! tmp_buf) {
                free(bufferdata);
                printf("failed to realloc\n");
                return false;
            }
            buffer_data = tmp_buf;
            buffer_size += CHUNK_SIZE ;

        }
        memcpy(bufferdata+total_read-read,buffer,read);
      }
      ... 
    }

It's hard to tell where the error is, there's some code missing here and there.

if(read == 4096) { looks like a culprit, what if nextBlock, returned 4000 on one iteration, and 97 on the next ? Now you need to store 4097 bytes but you don't reallocate the buffer to accomodate for it.

You need to accumulate the bytes, and realloc whenever you pass a 4096 boundary.
something like:

#define CHUNK_SIZE 4096
int total_read = 0;
int buffer_size = CHUNK_SIZE ;
char *bufferdata = malloc(CHUNK_SIZE );
char buffer[CHUNK_SIZE];
while( ... )
{

    // int nextBlock( voidp _buffer, unsigned _length );
    read=nextBlock( buffer, CHUNK_SIZE );

    if( read > 0 )
    {
        total_read += read;
        if(buffer_size < total_read) {
           // let's go for another chunk
            char *tmp_buf;
            tmp_buf= (char*)realloc(bufferdata, buffer_size + CHUNK_SIZE );
            if (! tmp_buf) {
                free(bufferdata);
                printf("failed to realloc\n");
                return false;
            }
            buffer_data = tmp_buf;
            buffer_size += CHUNK_SIZE ;

        }
        memcpy(bufferdata+total_read-read,buffer,read);
      }
      ... 
    }
冷情妓 2024-09-15 01:31:54

一些评论:

请定义或 const 4096。如果您需要更改它,您将会被烧毁。 realloc 链接是获取缓冲区的极其低效的方法。有什么办法可以预取大小并一次获取所有内容吗?也许不是,但当我看到 realloc() 时,我总是感到畏缩。我还想知道 kZipBufferSize 是什么,以及它是否像其他计数一样以字节为单位。另外,bufferdatawrite到底是什么?我假设它是源数据,但我想查看它的声明以确保它不是内存对齐问题 - 这有点像这种感觉。或者由于大小调整不当而导致缓冲区溢出。

最后,您确定 nextBlock 不会以某种方式超出内存吗?这是代码中的另一个潜在弱点。

A few comments:

Please define or const 4096. You will get burned if you ever need to change this. realloc chaining is an extremely inefficient way to get a buffer. Any way you could prefetch the size and grab it all at once? perhaps not, but I always cringe when i see realloc(). I'd also like to know what kZipBufferSize is and if it's in bytes like the rest of your counts. Also, what exactly is bufferdatawrite? I'm assuming it's source data, but I'd like to see it's declaration to make sure it's not a memory alignment issue - which is kinda what this feels like. Or a buffer overrun due to bad sizing.

Finally, are you sure they nextBlock isn't overruning memory some how? This is another point of potential weakness in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文