与内存打交道时的错误 - mremap_chunk:断言 [英] Error when dealing with memory - mremap_chunk: Assertion

查看:1928
本文介绍了与内存打交道时的错误 - mremap_chunk:断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是我的previous职位,但这里的问题是不同的。

It seems like my previous post but issue here is different ..

这是问题的C结构 -

This is the C structure for problem -

typedef struct ip_esp_private {         /* keep track of things privately */
u_int32_t type;        
u_int32_t ivlen;       
u_int32_t icvlen;      
u_int32_t keylen;       /* length of "Encryption key */
u_int32_t akeylen;      /*length of authn key */
u_int32_t key[0];       /* encryption key and authentication key both */

} esp_private; 

中提供的值来构建在运行时内容如下: -

The values are provided to structure contents at run time as follows -

  case 'k':       /* Key */
            length = stringargument(arg, &temp);
            priv->keylen = length;


            priv = (esp_private *)realloc(priv,
                            sizeof(esp_private)+/*length*/priv->keylen); 
             /*This one is edited */


        //  if(priv->akeylen)       
          //        memmove(&priv->key[priv->keylen],
                 //                    &priv->key[0],priv->akeylen);
   /*These three are commented*/     

       memcpy(&priv->key[0], temp, priv->keylen);
            pack->private = priv;
             pack->modified |= ESP_MOD_KEY;
            break;



    case 'K':       /* Authentication  Key */  
            length = stringargument(arg, &temp);
            priv->akeylen = length; // marked line(explained below)

            priv = (esp_private *)realloc(priv,
                            sizeof(esp_private)+/*length*/priv->keylen+priv->akeylen);
           /*this one edited too */ 


           memcpy(&priv->key[priv->keylen/sizeof(u_int32_t)],
                                             temp,priv->akeylen);
            pack->private = priv;
            pack->modified |= ESP_MOD_KEY;

现在有一种使用认证密钥的值的函数。

Now there is a function which uses the value of authentication key.

函数的相关部分是 -

The relevant part of the function is -

    if (!epriv->akeylen) {
            key = &fakekey;
            keylen = 1;
    } else {
            key = (u_int8_t *)malloc(epriv->akeylen);
            memcpy(key,&epriv->key[epriv->keylen/sizeof(u_int32_t)]
                             ,epriv->akeylen);

现在,当我试图运行下面的程序,收到此错误,对此,我也没办法。

Now when I tried to run the following program , getting this error about which I have no idea.

     sendip: malloc.c:3574: mremap_chunk: Assertion `((size + offset)
                                  & (mp_.pagesize-1)) == 0' failed.

我想可能是有在功能部分的错误,但它到底是什么我不知道,
因为当我评论标记线(上述)的 akeylen 为空
所以服用了 fakekey 值和程序运行正常。

I think may be there is a error in function part but what exactly it is I am not sure, because when I comment the marked line (mentioned above) the akeylen is null so taking that fakekey value and program runs fine.

编辑1:

我已经编辑了code。在三个地方(也编辑在上面code)。

I have edited the code at three places (also edited in the above code ).

现在节目的作品,但出现不一致的输出。

Now program works but an inconsistent output occurs.

输入:

 Encryption key - qwerty

 Authentication key - abcdef

输出:

  Encryption key - qwerab

  Authentication key - abcdef

的情况更加清晰了。

The situation is more clear now .

这意味着现在的问题是肯定存在的的realloc 语句。

The problem it means is surely there at realloc statements .

请在此建议。

起初我添加长度在两个的realloc 语句,但现在我改成了 priv-> KEYLEN 在第一地方和 priv-> KEYLEN + priv-> akeylen 在SECONE地方。

Initially I added length at both realloc statements but now I changed it to priv->keylen at first place and priv->keylen+priv->akeylen at secone place.

但仍有一些需要改进

这是为什么???覆盖

推荐答案

由于键[0]结构黑客似乎包含空间这两个键,你需要为分配内存,太多。在这两种情况下('K'和'K')

Since the key[0] struct hack appears to contain space for both keys, you'll need to allocate memory for both, too. In both cases ('k' and 'K' )

priv = realloc(priv, sizeof *priv +priv->keylen+priv->akeylen);

在串联的两个键,它是最容易投的u_int32_t钥匙插入字符指针,做算术上一句:

When concatenating the two keys, it is easiest to cast the u_int32_t key into a character pointer and do arithmatic on that one:

memcpy ( priv->key, source1, sizeofsource1);
/* and */ 
memcpy ( ((char*) priv->key) +priv->keylen, source2, sizeofsource2);

[和用于的memmove类似()]
在程序中强制转换的其余部分可以被删除。

[and similar for the memmove()] The rest of the casts in your program can be removed.

这篇关于与内存打交道时的错误 - mremap_chunk:断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆