X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;ds=sidebyside;f=cvar.c;h=7936c92356be359c13748eca7afbec3250df8ac2;hb=7b70abc823ed3391ea31938fbc2188bc2bf45d12;hp=344512d0946f6b1bce6ff8666f648f42ae687791;hpb=bbb80fb5e4edbd1a226bdc0e15d6f45efa11c67b;p=xonotic%2Fdarkplaces.git diff --git a/cvar.c b/cvar.c index 344512d0..7936c923 100644 --- a/cvar.c +++ b/cvar.c @@ -492,46 +492,41 @@ void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *)) variable->callback = callback; } -void Cvar_RegisterAlias(cvar_t *variable, const char *alias ) +void Cvar_RegisterVirtual(cvar_t *variable, const char *name ) { cvar_state_t *cvars = &cvars_all; cvar_hash_t *hash; int hashindex; - if(!*alias) + if(!*name) { - Con_Printf("Cvar_RegisterAlias: invalid alias name\n"); + Con_Printf("Cvar_RegisterVirtual: invalid virtual cvar name\n"); return; } // check for overlap with a command - if (Cmd_Exists(cmd_local, alias)) + if (Cmd_Exists(cmd_local, name)) { - Con_Printf("Cvar_RegisterAlias: %s is a command\n", alias); + Con_Printf("Cvar_RegisterVirtual: %s is a command\n", name); return; } - if(Cvar_FindVar(&cvars_all, alias, 0)) + if(Cvar_FindVar(&cvars_all, name, 0)) { - Con_Printf("Cvar_RegisterAlias: %s is a cvar\n", alias); + Con_Printf("Cvar_RegisterVirtual: %s is a cvar\n", name); return; } - if(!variable->aliases) - variable->aliases = (char **)Z_Malloc(sizeof(char *) * 2); // For NULL terminator - else - variable->aliases = (char **)Mem_Realloc(zonemempool, variable->aliases, sizeof(char *) * (variable->aliases_size + 1)); - - variable->aliases[variable->aliases_size + 1] = NULL; - - // Add to it - variable->aliases[variable->aliases_size] = (char *)Z_Malloc(strlen(alias) + 1); - memcpy(variable->aliases[variable->aliases_size], alias, strlen(alias) + 1); - variable->aliases_size++; + // Resize the variable->aliases list to have room for another entry and a null terminator. + // This zero-pads when resizing, so we don't need to write the NULL terminator manually here. + // Also if aliases is NULL this allocates fresh for the correct size, so it's fine to just do this. + variable->aliases = (char **)Z_Realloc(variable->aliases, sizeof(char *) * (variable->aliases_size + 2)); + // Add the new alias, and increment the number of aliases in the list + variable->aliases[variable->aliases_size++] = (char *)Z_strdup(name); // link to head of list in this hash table index hash = (cvar_hash_t *)Z_Malloc(sizeof(cvar_hash_t)); - hashindex = CRC_Block((const unsigned char *)alias, strlen(alias)) % CVAR_HASHSIZE; + hashindex = CRC_Block((const unsigned char *)name, strlen(name)) % CVAR_HASHSIZE; hash->next = cvars->hashtable[hashindex]; cvars->hashtable[hashindex] = hash; hash->cvar = variable; @@ -731,7 +726,6 @@ cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int f cvar->aliases = NULL; cvar->aliases_size = 0; cvar->initstate = NULL; - memset(cvar->aliases, 0, sizeof(char *)); if(newdescription && *newdescription) cvar->description = (char *)Mem_strdup(zonemempool, newdescription);