Skip to content

Commit

Permalink
Replace loop/if condition and reduce signed comparison warnings in av…
Browse files Browse the repository at this point in the history
…rftfi.c

Change (int) poll_index >= (int) addr to poll_index+1 > addr as the former
might turn out to be implementation-defined. The latter is always defined
and what we want here (poll_index+1 won't overflow). Originally, the
condition was poll_index > addr-1, which was always false for addr=0 owing to
2^n modulo arithmetic of unsigned.

Also changed a few comparisons so they no longer are between signed
and unsigned integers reducing compiler warnings.
  • Loading branch information
stefanrueger committed Aug 20, 2022
1 parent f8dd9bc commit 56aae55
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/avrftdi.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ static int avrftdi_transmit_bb(PROGRAMMER * pgm, unsigned char mode, const unsig
size_t blocksize = pdata->rx_buffer_size/2; // we are reading 2 bytes per data byte

// determine a maximum size of data block
size_t max_size = MIN(pdata->ftdic->max_packet_size,pdata->tx_buffer_size);
size_t max_size = MIN(pdata->ftdic->max_packet_size, (unsigned int) pdata->tx_buffer_size);
// select block size so that resulting commands does not exceed max_size if possible
blocksize = MAX(1,(max_size-7)/((8*2*6)+(8*1*2)));
//avrdude_message(MSG_INFO, "blocksize %d \n",blocksize);
Expand All @@ -368,9 +368,8 @@ static int avrftdi_transmit_bb(PROGRAMMER * pgm, unsigned char mode, const unsig
// (8*1) inputs per data byte, 2 transmit bytes per input (GET_BITS_LOW/HIGH),
// 1x SEND_IMMEDIATE
int len = 0;
int i;

for(i = 0 ; i< transfer_size; i++) {
for(size_t i = 0 ; i < transfer_size; i++) {
len += set_data(pgm, send_buffer + len, buf[written+i], (mode & MPSSE_DO_READ) != 0);
}

Expand All @@ -387,14 +386,14 @@ static int avrftdi_transmit_bb(PROGRAMMER * pgm, unsigned char mode, const unsig
E(ftdi_write_data(pdata->ftdic, send_buffer, len) != len, pdata->ftdic);
if (mode & MPSSE_DO_READ) {
int n;
int k = 0;
size_t k = 0;
do {
n = ftdi_read_data(pdata->ftdic, &recv_buffer[k], 2*16*transfer_size - k);
E(n < 0, pdata->ftdic);
k += n;
} while (k < transfer_size);

for(i = 0 ; i< transfer_size; i++) {
for(size_t i = 0 ; i< transfer_size; i++) {
data[written + i] = extract_data(pgm, recv_buffer, i);
}
}
Expand Down Expand Up @@ -437,15 +436,15 @@ static int avrftdi_transmit_mpsse(avrftdi_t* pdata, unsigned char mode, const un
{
size_t transfer_size = (remaining > blocksize) ? blocksize : remaining;

E(ftdi_write_data(pdata->ftdic, (unsigned char*)&buf[written], transfer_size) != transfer_size, pdata->ftdic);
E((size_t) ftdi_write_data(pdata->ftdic, (unsigned char*)&buf[written], transfer_size) != transfer_size, pdata->ftdic);
#if 0
if(remaining < blocksize)
E(ftdi_write_data(pdata->ftdic, &si, sizeof(si)) != sizeof(si), pdata->ftdic);
#endif

if (mode & MPSSE_DO_READ) {
int n;
int k = 0;
size_t k = 0;
do {
n = ftdi_read_data(pdata->ftdic, &data[written + k], transfer_size - k);
E(n < 0, pdata->ftdic);
Expand Down Expand Up @@ -1015,7 +1014,7 @@ static int avrftdi_flash_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
return -1;
}

if(page_size != m->page_size) {
if(page_size != (unsigned int) m->page_size) {
log_warn("Parameter page_size is %d, ", page_size);
log_warn("but m->page_size is %d. Using the latter.\n", m->page_size);
}
Expand Down Expand Up @@ -1059,11 +1058,11 @@ static int avrftdi_flash_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
/* find a poll byte. We cannot poll a value of 0xff, so look
* for a value != 0xff
*/
for(poll_index = addr+len-1; (int) poll_index >= (int) addr; poll_index--)
for(poll_index = addr+len-1; poll_index+1 > addr; poll_index--)
if(m->buf[poll_index] != 0xff)
break;

if((int) poll_index >= (int) addr) {
if(poll_index+1 > addr) {
buf_size = bufptr - buf;

if(verbose > TRACE)
Expand Down Expand Up @@ -1104,12 +1103,11 @@ static int avrftdi_flash_read(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
unsigned int page_size, unsigned int addr, unsigned int len)
{
OPCODE * readop;
int byte, word;

unsigned int buf_size = 4 * len + 4;
unsigned char* o_buf = alloca(buf_size);
unsigned char* i_buf = alloca(buf_size);
unsigned int index;


memset(o_buf, 0, buf_size);
memset(i_buf, 0, buf_size);
Expand All @@ -1128,7 +1126,7 @@ static int avrftdi_flash_read(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
return -1;

/* word addressing! */
for(word = addr/2, index = 0; word < (addr + len)/2; word++)
for(unsigned int word = addr/2, index = 0; word < (addr + len)/2; word++)
{
/* one byte is transferred via a 4-byte opcode.
* TODO: reduce magic numbers
Expand Down Expand Up @@ -1159,7 +1157,7 @@ static int avrftdi_flash_read(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
memset(&m->buf[addr], 0, page_size);

/* every (read) op is 4 bytes in size and yields one byte of memory data */
for(byte = 0; byte < page_size; byte++) {
for(unsigned int byte = 0; byte < page_size; byte++) {
if(byte & 1)
readop = m->op[AVR_OP_READ_HI];
else
Expand Down

0 comments on commit 56aae55

Please sign in to comment.