From ef10e9e4031328f35da52274cb504c3a78f24d2f Mon Sep 17 00:00:00 2001 From: Tom Haber Date: Fri, 22 Jan 2010 17:08:15 +0100 Subject: [PATCH] avr: Construct the pin table more carefully --- src/Device.cpp | 2 +- src/Device.h | 8 +++++--- src/DeviceSettings.cpp | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Device.cpp b/src/Device.cpp index 3f7526f..d97b9b0 100644 --- a/src/Device.cpp +++ b/src/Device.cpp @@ -138,7 +138,7 @@ namespace avr { pins.resize( num ); Pin *pin = new Pin(); - pins[ id ] = pin; + pins[ id - 1 ] = pin; // Multiple names? if( name[0] == '[' ) { diff --git a/src/Device.h b/src/Device.h index 0c6263c..831e5a3 100644 --- a/src/Device.h +++ b/src/Device.h @@ -78,7 +78,7 @@ namespace avr { */ void reset(unsigned int type); - Pin *getPin(int id) const; + Pin *getPin(unsigned int id) const; Pin *getPin(const std::string & name) const; public: @@ -116,8 +116,10 @@ namespace avr { friend class DeviceSettings; }; - inline Pin *Device::getPin(int id) const { - return pins[id]; + inline Pin *Device::getPin(unsigned int id) const { + if( id >= pins.size() ) + return 0; + return pins[ id - 1 ]; } } diff --git a/src/DeviceSettings.cpp b/src/DeviceSettings.cpp index 5caef58..159237b 100644 --- a/src/DeviceSettings.cpp +++ b/src/DeviceSettings.cpp @@ -389,12 +389,21 @@ namespace avr { } void DeviceSettings::parsePackage(Device *dev, xmlNode *package) { - int numPins = getIntAttribute(package, "pins", 10, true); + unsigned int numPins = getIntAttribute(package, "pins", 10, true); for(xmlNode *node = package->children; node != 0; node = node->next) { if( node->type == XML_ELEMENT_NODE ) { char *n = getAttribute(node, "name", true); - unsigned int num = getIntAttribute(node, "id", 10, true); - dev->addPin(num, n, numPins); + unsigned int id = getIntAttribute(node, "id", 10, true); + if( id > numPins ) + throw ParseException( + util::format("Pin %d (%s) goes beyond number of pins (%d)") + % id % n % numPins ); + + if( dev->getPin(id) != 0 ) + throw ParseException( + util::format("Pin %d (%s) already exists") % id % n ); + + dev->addPin(id, n, numPins); freeAttribute(n); } } -- 2.11.4.GIT