When `dbus-daemon` sets more than 128 directories for `XDG_DATA_DIRS`,
none of the elements in `new_dirs` will be `NULL`, which resulted in
these loops reading out-of-bounds (undefined behaviour). In practice
this led to a crash.
To avoid this, make sure to stop iteration at the end of the array.
[smcv: Expanded commit message]
Resolves: dbus/dbus#481
/* Look for directories in both the old and new sets, if
* we find one, move its data into the new set.
*/
- for (i = 0; new_dirs[i]; i++)
+ for (i = 0; i < MAX_DIRS_TO_WATCH && new_dirs[i]; i++)
{
for (j = 0; j < num_wds; j++)
{
}
}
- for (i = 0; new_dirs[i]; i++)
+ for (i = 0; i < MAX_DIRS_TO_WATCH && new_dirs[i]; i++)
{
if (new_wds[i] == -1)
{
/* Look for directories in both the old and new sets, if
* we find one, move its data into the new set.
*/
- for (i = 0; new_dirs[i]; i++)
+ for (i = 0; i < MAX_DIRS_TO_WATCH && new_dirs[i]; i++)
{
for (j = 0; j < num_fds; j++)
{
}
}
- for (i = 0; new_dirs[i]; i++)
+ for (i = 0; i < MAX_DIRS_TO_WATCH && new_dirs[i]; i++)
{
if (new_fds[i] == -1)
{