dcb.c:dcb_hashtable_stats:division by zero
hashtable.c:hashtable_get_stats: NULL-pointer reference
service.c:serviceStartPort:set service->users NULL to avoid referring to freed memory
users.c:dcb_usersPrintf: NULL-pointer reference
debugcmd.c:convert_arg: changed return value to 1 in case of error, 0 (==NULL) is valid but it indicates that there are no users loaded.
	execute_cmd: fixed command handling
This commit is contained in:
VilhoRaatikka
2014-12-31 21:06:28 +02:00
parent 9b52f5e6ba
commit f4f537a3e2
5 changed files with 66 additions and 45 deletions

View File

@ -1610,7 +1610,9 @@ void dcb_hashtable_stats(
hashsize); hashsize);
dcb_printf(dcb, "\tNo. of entries: %d\n", total); dcb_printf(dcb, "\tNo. of entries: %d\n", total);
dcb_printf(dcb, "\tAverage chain length: %.1f\n", (float)total / hashsize); dcb_printf(dcb,
"\tAverage chain length: %.1f\n",
(hashsize == 0 ? (float)hashsize : (float)total / hashsize));
dcb_printf(dcb, "\tLongest chain length: %d\n", longest); dcb_printf(dcb, "\tLongest chain length: %d\n", longest);
} }

View File

@ -449,28 +449,33 @@ void hashtable_get_stats(
int i; int i;
int j; int j;
ht = (HASHTABLE *)table; *nelems = 0;
CHK_HASHTABLE(ht); *longest = 0;
*nelems = 0; *hashsize = 0;
*longest = 0;
hashtable_read_lock(ht);
for (i = 0; i < ht->hashsize; i++) if (table != NULL)
{ {
j = 0; ht = (HASHTABLE *)table;
entries = ht->entries[i]; CHK_HASHTABLE(ht);
while (entries) hashtable_read_lock(ht);
for (i = 0; i < ht->hashsize; i++)
{ {
j++; j = 0;
entries = entries->next; entries = ht->entries[i];
while (entries)
{
j++;
entries = entries->next;
}
*nelems += j;
if (j > *longest) {
*longest = j;
}
} }
*nelems += j; *hashsize = ht->hashsize;
if (j > *longest) { hashtable_read_unlock(ht);
*longest = j;
}
} }
*hashsize = ht->hashsize;
hashtable_read_unlock(ht);
} }

View File

@ -217,6 +217,7 @@ GWPROTOCOL *funcs;
service->name))); service->name)));
hashtable_free(service->users->data); hashtable_free(service->users->data);
free(service->users); free(service->users);
service->users = NULL;
dcb_free(port->listener); dcb_free(port->listener);
port->listener = NULL; port->listener = NULL;
goto retblock; goto retblock;

View File

@ -183,32 +183,41 @@ char *sep;
void *user; void *user;
dcb_printf(dcb, "Users table data\n"); dcb_printf(dcb, "Users table data\n");
dcb_hashtable_stats(dcb, users->data);
if ((iter = hashtable_iterator(users->data)) != NULL)
{
dcb_printf(dcb, "User names: ");
sep = "";
if (users->usersCustomUserFormat != NULL) { if (users == NULL || users->data == NULL)
while ((user = hashtable_next(iter)) != NULL) {
{ dcb_printf(dcb, "Users table is empty\n");
char *custom_user; }
custom_user = users->usersCustomUserFormat(user); else
if (custom_user) { {
dcb_printf(dcb, "%s%s", sep, custom_user); dcb_hashtable_stats(dcb, users->data);
free(custom_user);
if ((iter = hashtable_iterator(users->data)) != NULL)
{
dcb_printf(dcb, "User names: ");
sep = "";
if (users->usersCustomUserFormat != NULL) {
while ((user = hashtable_next(iter)) != NULL)
{
char *custom_user;
custom_user = users->usersCustomUserFormat(user);
if (custom_user) {
dcb_printf(dcb, "%s%s", sep, custom_user);
free(custom_user);
sep = ", ";
}
}
} else {
while ((user = hashtable_next(iter)) != NULL)
{
dcb_printf(dcb, "%s%s", sep, (char *)user);
sep = ", "; sep = ", ";
} }
} }
} else {
while ((user = hashtable_next(iter)) != NULL)
{
dcb_printf(dcb, "%s%s", sep, (char *)user);
sep = ", ";
}
}
dcb_printf(dcb, "\n"); hashtable_iterator_free(iter);
hashtable_iterator_free(iter); }
} }
dcb_printf(dcb, "\n");
} }

View File

@ -678,7 +678,7 @@ SERVICE *service;
if (service) if (service)
return (unsigned long)(service->users); return (unsigned long)(service->users);
else else
return 0; return 1; /*< invalid argument */
} }
return rval; return rval;
case ARG_TYPE_DCB: case ARG_TYPE_DCB:
@ -886,11 +886,15 @@ bool in_space = false;
break; break;
case 1: case 1:
arg1 = convert_arg(cli->mode, args[2],cmds[i].options[j].arg_types[0]); arg1 = convert_arg(cli->mode, args[2],cmds[i].options[j].arg_types[0]);
if (arg1) if (arg1 == 0x1)
cmds[i].options[j].fn(dcb, arg1); {
else
dcb_printf(dcb, "Invalid argument: %s\n", dcb_printf(dcb, "Invalid argument: %s\n",
args[2]); args[2]);
}
else
{
cmds[i].options[j].fn(dcb, arg1);
}
break; break;
case 2: case 2:
arg1 = convert_arg(cli->mode, args[2],cmds[i].options[j].arg_types[0]); arg1 = convert_arg(cli->mode, args[2],cmds[i].options[j].arg_types[0]);