Commit 4c3b672c authored by pkasting's avatar pkasting Committed by Commit bot

Fix type truncation warnings in HID code.

GetShortData() and IOHIDElementGetUsage() both return uint32s, so raise the size
of various containing types to uint32.

I tried to look in the USB HID spec to see if any of the affected fields here
were limited to e.g. <2^16, meaning that it'd be safe to just cast down to the
existing uint16s instead, but I couldn't find any limits.

BUG=81439
TEST=none

Review URL: https://codereview.chromium.org/656583003

Cr-Commit-Position: refs/heads/master@{#300201}
parent f5c85cf6
...@@ -111,7 +111,7 @@ void HidConnectionLinux::PlatformGetFeatureReport( ...@@ -111,7 +111,7 @@ void HidConnectionLinux::PlatformGetFeatureReport(
const ReadCallback& callback) { const ReadCallback& callback) {
// The first byte of the destination buffer is the report ID being requested // The first byte of the destination buffer is the report ID being requested
// and is overwritten by the feature report. // and is overwritten by the feature report.
DCHECK_GT(device_info().max_feature_report_size, 0); DCHECK_GT(device_info().max_feature_report_size, 0u);
scoped_refptr<net::IOBufferWithSize> buffer( scoped_refptr<net::IOBufferWithSize> buffer(
new net::IOBufferWithSize(device_info().max_feature_report_size + 1)); new net::IOBufferWithSize(device_info().max_feature_report_size + 1));
buffer->data()[0] = report_id; buffer->data()[0] = report_id;
......
...@@ -158,10 +158,11 @@ TEST_F(HidConnectionTest, ReadWrite) { ...@@ -158,10 +158,11 @@ TEST_F(HidConnectionTest, ReadWrite) {
scoped_refptr<HidConnection> conn = connect_callback.WaitForConnection(); scoped_refptr<HidConnection> conn = connect_callback.WaitForConnection();
ASSERT_TRUE(conn.get()); ASSERT_TRUE(conn.get());
for (int i = 0; i < 8; ++i) { const char kBufferSize = 9;
scoped_refptr<IOBufferWithSize> buffer(new IOBufferWithSize(9)); for (char i = 0; i < 8; ++i) {
scoped_refptr<IOBufferWithSize> buffer(new IOBufferWithSize(kBufferSize));
buffer->data()[0] = 0; buffer->data()[0] = 0;
for (int j = 1; j < buffer->size(); ++j) { for (char j = 1; j < kBufferSize; ++j) {
buffer->data()[j] = i + j - 1; buffer->data()[j] = i + j - 1;
} }
...@@ -174,7 +175,7 @@ TEST_F(HidConnectionTest, ReadWrite) { ...@@ -174,7 +175,7 @@ TEST_F(HidConnectionTest, ReadWrite) {
ASSERT_TRUE(read_callback.WaitForResult()); ASSERT_TRUE(read_callback.WaitForResult());
ASSERT_EQ(9UL, read_callback.size()); ASSERT_EQ(9UL, read_callback.size());
ASSERT_EQ(0, read_callback.buffer()->data()[0]); ASSERT_EQ(0, read_callback.buffer()->data()[0]);
for (int j = 1; j < buffer->size(); ++j) { for (char j = 1; j < kBufferSize; ++j) {
ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]); ASSERT_EQ(i + j - 1, read_callback.buffer()->data()[j]);
} }
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/files/file.h" #include "base/files/file.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/numerics/safe_conversions.h"
#include "base/profiler/scoped_profile.h" #include "base/profiler/scoped_profile.h"
#include "base/win/object_watcher.h" #include "base/win/object_watcher.h"
...@@ -135,8 +136,8 @@ void HidConnectionWin::PlatformRead( ...@@ -135,8 +136,8 @@ void HidConnectionWin::PlatformRead(
const HidConnection::ReadCallback& callback) { const HidConnection::ReadCallback& callback) {
// Windows will always include the report ID (including zero if report IDs // Windows will always include the report ID (including zero if report IDs
// are not in use) in the buffer. // are not in use) in the buffer.
scoped_refptr<net::IOBufferWithSize> buffer = scoped_refptr<net::IOBufferWithSize> buffer = new net::IOBufferWithSize(
new net::IOBufferWithSize(device_info().max_input_report_size + 1); base::checked_cast<int>(device_info().max_input_report_size + 1));
scoped_refptr<PendingHidTransfer> transfer(new PendingHidTransfer( scoped_refptr<PendingHidTransfer> transfer(new PendingHidTransfer(
buffer, buffer,
base::Bind(&HidConnectionWin::OnReadComplete, this, buffer, callback))); base::Bind(&HidConnectionWin::OnReadComplete, this, buffer, callback)));
...@@ -167,8 +168,8 @@ void HidConnectionWin::PlatformWrite(scoped_refptr<net::IOBuffer> buffer, ...@@ -167,8 +168,8 @@ void HidConnectionWin::PlatformWrite(scoped_refptr<net::IOBuffer> buffer,
void HidConnectionWin::PlatformGetFeatureReport(uint8_t report_id, void HidConnectionWin::PlatformGetFeatureReport(uint8_t report_id,
const ReadCallback& callback) { const ReadCallback& callback) {
// The first byte of the destination buffer is the report ID being requested. // The first byte of the destination buffer is the report ID being requested.
scoped_refptr<net::IOBufferWithSize> buffer = scoped_refptr<net::IOBufferWithSize> buffer = new net::IOBufferWithSize(
new net::IOBufferWithSize(device_info().max_feature_report_size + 1); base::checked_cast<int>(device_info().max_feature_report_size + 1));
buffer->data()[0] = report_id; buffer->data()[0] = report_id;
scoped_refptr<PendingHidTransfer> transfer(new PendingHidTransfer( scoped_refptr<PendingHidTransfer> transfer(new PendingHidTransfer(
......
...@@ -36,9 +36,9 @@ struct HidDeviceInfo { ...@@ -36,9 +36,9 @@ struct HidDeviceInfo {
// Top-Level Collections information. // Top-Level Collections information.
std::vector<HidCollectionInfo> collections; std::vector<HidCollectionInfo> collections;
bool has_report_id; bool has_report_id;
uint16_t max_input_report_size; size_t max_input_report_size;
uint16_t max_output_report_size; size_t max_output_report_size;
uint16_t max_feature_report_size; size_t max_feature_report_size;
}; };
} // namespace device } // namespace device
......
...@@ -29,9 +29,9 @@ HidReportDescriptor::~HidReportDescriptor() {} ...@@ -29,9 +29,9 @@ HidReportDescriptor::~HidReportDescriptor() {}
void HidReportDescriptor::GetDetails( void HidReportDescriptor::GetDetails(
std::vector<HidCollectionInfo>* top_level_collections, std::vector<HidCollectionInfo>* top_level_collections,
bool* has_report_id, bool* has_report_id,
uint16_t* max_input_report_size, size_t* max_input_report_size,
uint16_t* max_output_report_size, size_t* max_output_report_size,
uint16_t* max_feature_report_size) { size_t* max_feature_report_size) {
DCHECK(top_level_collections); DCHECK(top_level_collections);
DCHECK(max_input_report_size); DCHECK(max_input_report_size);
DCHECK(max_output_report_size); DCHECK(max_output_report_size);
...@@ -45,16 +45,16 @@ void HidReportDescriptor::GetDetails( ...@@ -45,16 +45,16 @@ void HidReportDescriptor::GetDetails(
// Global tags data: // Global tags data:
HidUsageAndPage::Page current_usage_page = HidUsageAndPage::kPageUndefined; HidUsageAndPage::Page current_usage_page = HidUsageAndPage::kPageUndefined;
uint16_t current_report_count = 0; size_t current_report_count = 0;
uint16_t cached_report_count = 0; size_t cached_report_count = 0;
uint16_t current_report_size = 0; size_t current_report_size = 0;
uint16_t cached_report_size = 0; size_t cached_report_size = 0;
uint16_t current_input_report_size = 0; size_t current_input_report_size = 0;
uint16_t current_output_report_size = 0; size_t current_output_report_size = 0;
uint16_t current_feature_report_size = 0; size_t current_feature_report_size = 0;
// Local tags data: // Local tags data:
uint16_t current_usage = 0; uint32_t current_usage = 0;
for (std::vector<linked_ptr<HidReportDescriptorItem> >::const_iterator for (std::vector<linked_ptr<HidReportDescriptorItem> >::const_iterator
items_iter = items().begin(); items_iter = items().begin();
...@@ -65,10 +65,12 @@ void HidReportDescriptor::GetDetails( ...@@ -65,10 +65,12 @@ void HidReportDescriptor::GetDetails(
switch (current_item->tag()) { switch (current_item->tag()) {
// Main tags: // Main tags:
case HidReportDescriptorItem::kTagCollection: case HidReportDescriptorItem::kTagCollection:
if (!current_item->parent()) { if (!current_item->parent() &&
(current_usage <= std::numeric_limits<uint16_t>::max())) {
// This is a top-level collection. // This is a top-level collection.
HidCollectionInfo collection; HidCollectionInfo collection;
collection.usage = HidUsageAndPage(current_usage, current_usage_page); collection.usage = HidUsageAndPage(
static_cast<uint16_t>(current_usage), current_usage_page);
top_level_collections->push_back(collection); top_level_collections->push_back(collection);
} }
break; break;
...@@ -87,7 +89,7 @@ void HidReportDescriptor::GetDetails( ...@@ -87,7 +89,7 @@ void HidReportDescriptor::GetDetails(
// Global tags: // Global tags:
case HidReportDescriptorItem::kTagUsagePage: case HidReportDescriptorItem::kTagUsagePage:
current_usage_page = current_usage_page =
(HidUsageAndPage::Page)current_item->GetShortData(); static_cast<HidUsageAndPage::Page>(current_item->GetShortData());
break; break;
case HidReportDescriptorItem::kTagReportId: case HidReportDescriptorItem::kTagReportId:
if (top_level_collections->size() > 0) { if (top_level_collections->size() > 0) {
......
...@@ -29,9 +29,9 @@ class HidReportDescriptor { ...@@ -29,9 +29,9 @@ class HidReportDescriptor {
// together with max report sizes // together with max report sizes
void GetDetails(std::vector<HidCollectionInfo>* top_level_collections, void GetDetails(std::vector<HidCollectionInfo>* top_level_collections,
bool* has_report_id, bool* has_report_id,
uint16_t* max_input_report_size, size_t* max_input_report_size,
uint16_t* max_output_report_size, size_t* max_output_report_size,
uint16_t* max_feature_report_size); size_t* max_feature_report_size);
private: private:
std::vector<linked_ptr<HidReportDescriptorItem> > items_; std::vector<linked_ptr<HidReportDescriptorItem> > items_;
......
...@@ -298,18 +298,18 @@ class HidReportDescriptorTest : public testing::Test { ...@@ -298,18 +298,18 @@ class HidReportDescriptorTest : public testing::Test {
void ValidateDetails( void ValidateDetails(
const std::vector<HidCollectionInfo>& expected_collections, const std::vector<HidCollectionInfo>& expected_collections,
const bool expected_has_report_id, const bool expected_has_report_id,
const uint16_t expected_max_input_report_size, const size_t expected_max_input_report_size,
const uint16_t expected_max_output_report_size, const size_t expected_max_output_report_size,
const uint16_t expected_max_feature_report_size, const size_t expected_max_feature_report_size,
const uint8_t* bytes, const uint8_t* bytes,
size_t size) { size_t size) {
descriptor_ = new HidReportDescriptor(bytes, size); descriptor_ = new HidReportDescriptor(bytes, size);
std::vector<HidCollectionInfo> actual_collections; std::vector<HidCollectionInfo> actual_collections;
bool actual_has_report_id; bool actual_has_report_id;
uint16_t actual_max_input_report_size; size_t actual_max_input_report_size;
uint16_t actual_max_output_report_size; size_t actual_max_output_report_size;
uint16_t actual_max_feature_report_size; size_t actual_max_feature_report_size;
descriptor_->GetDetails(&actual_collections, descriptor_->GetDetails(&actual_collections,
&actual_has_report_id, &actual_has_report_id,
&actual_max_input_report_size, &actual_max_input_report_size,
......
...@@ -106,8 +106,11 @@ void GetCollectionInfos(IOHIDDeviceRef device, ...@@ -106,8 +106,11 @@ void GetCollectionInfos(IOHIDDeviceRef device,
HidCollectionInfo collection_info; HidCollectionInfo collection_info;
HidUsageAndPage::Page page = static_cast<HidUsageAndPage::Page>( HidUsageAndPage::Page page = static_cast<HidUsageAndPage::Page>(
IOHIDElementGetUsagePage(collection)); IOHIDElementGetUsagePage(collection));
uint16_t usage = IOHIDElementGetUsage(collection); uint32_t usage = IOHIDElementGetUsage(collection);
collection_info.usage = HidUsageAndPage(usage, page); if (usage > std::numeric_limits<uint16_t>::max())
continue;
collection_info.usage =
HidUsageAndPage(static_cast<uint16_t>(usage), page);
// Explore children recursively and retrieve their report IDs // Explore children recursively and retrieve their report IDs
GetReportIds(collection, &collection_info.report_ids); GetReportIds(collection, &collection_info.report_ids);
if (collection_info.report_ids.size() > 0) { if (collection_info.report_ids.size() > 0) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment