gecko-dev/ipc/glue/IPCMessageUtils.cpp
Andrew McCreight 17fc7463dd Bug 1268130, part 1 - Reimplement ByteLengthIsValid using CheckedInt. r=froydnj
The first check in ByteLengthIsValid() says "nsTArray only handles
sizes up to INT32_MAX", but the actual requirement is that the
capacity is no larger than UINT32_MAX. The check is overly restrictive
if sizeof(E) is 1 byte, and overly permissive if sizeof(E) is greater
than 2 bytes. I removed this check. Internal nsTArray invariants
should be enforced by nsTArray methods.

The second check is trying to check for overflow, but that should just
be done using CheckedInt.
2016-04-28 10:00:46 -07:00

24 lines
674 B
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "IPCMessageUtils.h"
#include "mozilla/CheckedInt.h"
namespace IPC {
bool
ByteLengthIsValid(uint32_t aNumElements, size_t aElementSize, int* aByteLength)
{
auto length = CheckedInt<int>(aNumElements) * aElementSize;
if (!length.isValid()) {
return false;
}
*aByteLength = length.value();
return true;
}
} // namespace IPC