I have a C structure like this :
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]; /* key itself */
// u_int32_t akey[0]; /*authn key itself */
u_int32_t *key; /* as suggested */
u_int32_t *akey; /* as suggested */
} esp_private;
Now the problem should be obvious from here when I try to write to key , the akey will be overwritten .
I am not sure how can I protect it ???
keylen
and akeylen
provide length of key
and akey
array and whole structure is to be filled at run time.
Please provide some suggestions .
EDIT :
yes ... u_int32_t is a typedef so dont bother about that
I have changed the zero length array to pointers as edited above.This is how I need to fill in the structure ..
case 'k': /* Key */ length = stringargument(arg, &temp); priv->keylen = length; priv->key=malloc(priv->keylen * sizeof(u_int32_t)); /*as suggested added this step*/ /*as suggested commented this step*/ priv = (esp_private *)realloc(priv, sizeof(esp_private) + length); memcpy(priv->key, temp, priv->keylen); pack->private = priv; pack->modified |= ESP_MOD_KEY; break; case 'K': /* Authentication Key */ length = stringargument(arg, &temp); priv->akeylen = length; priv->akey=malloc(priv->akeylen * sizeof(u_int32_t)); /*as suggested added this step*/ /*as suggested commented this step*/ priv = (esp_private *)realloc(priv, sizeof(esp_private) + length); memcpy(priv->akey, temp, priv->akeylen); pack->private = priv; pack->modified |= ESP_MOD_AKEY;
but not working out .. may be not re-allocating carefully please suggest on this edit also.
EDIT 2:
Even after commenting the realloc
as suggested , its not working out.The key
and akey
were supposed to take value mentioned as arg on command line but they are taking some random values.There is still something wrong here which need attention.
Please help me as the whole project has stuck in between just because of a minute error in this small piece of code.Need it soon ...