Skip to content

Commit

Permalink
COMMON: Fix possible out of bound reads in the debug channel
Browse files Browse the repository at this point in the history
Coverity issues 1038257 and 1038258.
  • Loading branch information
DrMcCoy committed Jun 25, 2013
1 parent 4e7d85f commit 3dcd587
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/common/debugman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ DECLARE_SINGLETON(Common::DebugManager)
namespace Common {

DebugManager::DebugManager() : _debugLevel(0) {
for (uint32 i = 0; i < 30; i++)
for (uint32 i = 0; i < kChannelCount; i++)
_channels[i].enabled = false;

addDebugChannel(kDebugGraphics, "GGraphics", "Global graphics debug channel");
Expand All @@ -67,7 +67,7 @@ bool DebugManager::addDebugChannel(uint32 channel, const UString &name,
return false;

int index = intLog2(channel);
if ((index < 0) || (index > 30))
if ((index < 0) || ((uint)index >= kChannelCount))
return false;

if (!_channels[index].name.empty())
Expand All @@ -90,7 +90,7 @@ void DebugManager::getDebugChannels(std::vector<UString> &names,
descriptions.clear();
nameLength = 0;

for (uint32 i = 0; i < 30; i++) {
for (uint32 i = 0; i < kChannelCount; i++) {
const Channel &channel = _channels[i];
if (channel.name.empty())
continue;
Expand All @@ -103,7 +103,7 @@ void DebugManager::getDebugChannels(std::vector<UString> &names,
}

void DebugManager::clearEngineChannels() {
for (uint32 i = 15; i < 30; i++) {
for (uint32 i = kGlobalChannelCount; i < kChannelCount; i++) {
Channel &channel = _channels[i];

ChannelMap::iterator c = _channelMap.find(channel.name);
Expand Down Expand Up @@ -138,7 +138,7 @@ uint32 DebugManager::parseChannelList(const UString &list) const {
}

void DebugManager::setEnabled(uint32 mask) {
for (uint32 i = 0; i < 30; i++, mask >>= 1)
for (uint32 i = 0; i < kChannelCount; i++, mask >>= 1)
_channels[i].enabled = (mask & 1) != 0;
}

Expand All @@ -150,7 +150,7 @@ bool DebugManager::isEnabled(uint32 level, uint32 channel) const {
return false;

int index = intLog2(channel);
if ((index < 0) || (index > 30))
if ((index < 0) || ((uint)index >= kChannelCount))
return false;

return _channels[index].enabled;
Expand Down
8 changes: 6 additions & 2 deletions src/common/debugman.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class DebugManager : public Singleton<DebugManager> {
void logString(const UString &str);

private:
static const uint kGlobalChannelCount = 15;
static const uint kEngineChannelCount = 15;
static const uint kChannelCount = kGlobalChannelCount + kEngineChannelCount;

/** A debug channel. */
struct Channel {
UString name; ///< The channel's name.
Expand All @@ -129,8 +133,8 @@ class DebugManager : public Singleton<DebugManager> {

typedef std::map<UString, uint32> ChannelMap;

Channel _channels[30]; ///< All debug channels.
ChannelMap _channelMap; ///< Debug channels indexed by name.
Channel _channels[kChannelCount]; ///< All debug channels.
ChannelMap _channelMap; ///< Debug channels indexed by name.

uint32 _debugLevel; ///< The current debug level.

Expand Down

0 comments on commit 3dcd587

Please sign in to comment.