Skip to content

Commit 452df00

Browse files
committed
Cleaned up warnings and fixed array overrun
Cleaned up warnings about unsigned types, unused variables, and declarations. Explicitly declare char as signed for cases when compiling with -funsigned-char. Fixed strict-aliasing issues in IPAddress.h. Fixed array overrun where EXTERNAL_NUM_INTERRUPTS was defined too small.
1 parent d003c53 commit 452df00

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

hardware/arduino/cores/arduino/HardwareSerial.cpp

+21-16
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@
5454
// to which to write the next incoming character and tail is the index of the
5555
// location from which to read.
5656
#if (RAMEND < 1000)
57-
#define SERIAL_BUFFER_SIZE 16
57+
#define SERIAL_BUFFER_SIZE 16U
5858
#else
59-
#define SERIAL_BUFFER_SIZE 64
59+
#define SERIAL_BUFFER_SIZE 64U
6060
#endif
6161

6262
struct ring_buffer
@@ -89,7 +89,7 @@ struct ring_buffer
8989

9090
inline void store_char(unsigned char c, ring_buffer *buffer)
9191
{
92-
int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
92+
unsigned int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
9393

9494
// if we should be storing the received character into the location
9595
// just before the tail (meaning that the head would advance to the
@@ -120,18 +120,20 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
120120
#endif
121121
{
122122
#if defined(UDR0)
123+
unsigned char c;
123124
if (bit_is_clear(UCSR0A, UPE0)) {
124-
unsigned char c = UDR0;
125+
c = UDR0;
125126
store_char(c, &rx_buffer);
126127
} else {
127-
unsigned char c = UDR0;
128+
c = UDR0;
128129
};
129130
#elif defined(UDR)
131+
unsigned char c;
130132
if (bit_is_clear(UCSRA, PE)) {
131-
unsigned char c = UDR;
133+
c = UDR;
132134
store_char(c, &rx_buffer);
133135
} else {
134-
unsigned char c = UDR;
136+
c = UDR;
135137
};
136138
#else
137139
#error UDR not defined
@@ -146,11 +148,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
146148
#define serialEvent1_implemented
147149
ISR(USART1_RX_vect)
148150
{
151+
unsigned char c;
149152
if (bit_is_clear(UCSR1A, UPE1)) {
150-
unsigned char c = UDR1;
153+
c = UDR1;
151154
store_char(c, &rx_buffer1);
152155
} else {
153-
unsigned char c = UDR1;
156+
c = UDR1;
154157
};
155158
}
156159
#endif
@@ -161,11 +164,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
161164
#define serialEvent2_implemented
162165
ISR(USART2_RX_vect)
163166
{
167+
unsigned char c;
164168
if (bit_is_clear(UCSR2A, UPE2)) {
165-
unsigned char c = UDR2;
169+
c = UDR2;
166170
store_char(c, &rx_buffer2);
167171
} else {
168-
unsigned char c = UDR2;
172+
c = UDR2;
169173
};
170174
}
171175
#endif
@@ -176,11 +180,12 @@ inline void store_char(unsigned char c, ring_buffer *buffer)
176180
#define serialEvent3_implemented
177181
ISR(USART3_RX_vect)
178182
{
183+
unsigned char c;
179184
if (bit_is_clear(UCSR3A, UPE3)) {
180-
unsigned char c = UDR3;
185+
c = UDR3;
181186
store_char(c, &rx_buffer3);
182187
} else {
183-
unsigned char c = UDR3;
188+
c = UDR3;
184189
};
185190
}
186191
#endif
@@ -365,7 +370,6 @@ void HardwareSerial::begin(unsigned long baud)
365370
void HardwareSerial::begin(unsigned long baud, byte config)
366371
{
367372
uint16_t baud_setting;
368-
uint8_t current_config;
369373
bool use_u2x = true;
370374

371375
#if F_CPU == 16000000UL
@@ -453,13 +457,14 @@ int HardwareSerial::read(void)
453457
void HardwareSerial::flush()
454458
{
455459
// UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT
456-
while (transmitting && ! (*_ucsra & _BV(TXC0)));
460+
while (transmitting && ! (*_ucsra & _BV(TXC0)))
461+
;
457462
transmitting = false;
458463
}
459464

460465
size_t HardwareSerial::write(uint8_t c)
461466
{
462-
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
467+
unsigned int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
463468

464469
// If the output buffer is full, there's nothing for it other than to
465470
// wait for the interrupt handler to empty it a bit

hardware/arduino/cores/arduino/IPAddress.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ class IPAddress : public Printable {
4848

4949
// Overloaded cast operator to allow IPAddress objects to be used where a pointer
5050
// to a four-byte uint8_t array is expected
51-
operator uint32_t() { return *((uint32_t*)_address); };
52-
bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
51+
operator uint32_t() { return ((uint32_t)_address[0] << 0 |
52+
(uint32_t)_address[1] << 8 |
53+
(uint32_t)_address[2] << 16 |
54+
(uint32_t)_address[3] << 24); }
55+
bool operator==(const IPAddress& addr) { return (_address[0] == addr._address[0] &&
56+
_address[1] == addr._address[1] &&
57+
_address[2] == addr._address[2] &&
58+
_address[3] == addr._address[3]); }
5359
bool operator==(const uint8_t* addr);
5460

5561
// Overloaded index operator to allow getting and setting individual octets of the address

hardware/arduino/cores/arduino/Stream.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ float Stream::parseFloat(char skipChar){
176176
boolean isNegative = false;
177177
boolean isFraction = false;
178178
long value = 0;
179-
char c;
179+
signed char c;
180180
float fraction = 1.0;
181181

182182
c = peekNextDigit();

hardware/arduino/cores/arduino/wiring_private.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ extern "C"{
5252
#define EXTERNAL_INT_6 6
5353
#define EXTERNAL_INT_7 7
5454

55-
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
55+
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || \
56+
defined(__AVR_ATmega128RFA1__) || defined(__AVR_ATmega256RFR2__)
5657
#define EXTERNAL_NUM_INTERRUPTS 8
5758
#elif defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__)
5859
#define EXTERNAL_NUM_INTERRUPTS 3

0 commit comments

Comments
 (0)