Skip to content

Commit

Permalink
Fixed some warnings. (closing fukuchi#89 and fukuchi#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
fukuchi committed Oct 7, 2017
1 parent c4e3b9c commit 0649403
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 45 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2017.10.08 Kentaro Fukuchi <[email protected]>
[master]
* qrenc.c, qrinput.c, rsecc.c, tests/common.c, tests/decoder.c:
- Fixed some warnings. (closing #89 and #102)

2017.10.06 Kentaro Fukuchi <[email protected]>
[master]
* tests/test_{all,basic}.sh:
Expand Down
42 changes: 21 additions & 21 deletions qrenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ static int writePNG(const QRcode *qrcode, const char *outfile, enum imageType ty

realwidth = (qrcode->width + margin * 2) * size;
if(type == PNG_TYPE) {
row = (unsigned char *)malloc((realwidth + 7) / 8);
row = (unsigned char *)malloc((size_t)((realwidth + 7) / 8));
} else if(type == PNG32_TYPE) {
row = (unsigned char *)malloc(realwidth * 4);
row = (unsigned char *)malloc((size_t)realwidth * 4);
} else {
fprintf(stderr, "Internal error.\n");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -355,15 +355,15 @@ static int writePNG(const QRcode *qrcode, const char *outfile, enum imageType ty
png_init_io(png_ptr, fp);
if(type == PNG_TYPE) {
png_set_IHDR(png_ptr, info_ptr,
realwidth, realwidth,
(unsigned int)realwidth, (unsigned int)realwidth,
1,
PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
} else {
png_set_IHDR(png_ptr, info_ptr,
realwidth, realwidth,
(unsigned int)realwidth, (unsigned int)realwidth,
8,
PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE,
Expand All @@ -378,15 +378,15 @@ static int writePNG(const QRcode *qrcode, const char *outfile, enum imageType ty

if(type == PNG_TYPE) {
/* top margin */
memset(row, 0xff, (realwidth + 7) / 8);
memset(row, 0xff, (size_t)((realwidth + 7) / 8));
for(y = 0; y < margin * size; y++) {
png_write_row(png_ptr, row);
}

/* data */
p = qrcode->data;
for(y = 0; y < qrcode->width; y++) {
memset(row, 0xff, (realwidth + 7) / 8);
memset(row, 0xff, (size_t)((realwidth + 7) / 8));
q = row;
q += margin * size / 8;
bit = 7 - (margin * size % 8);
Expand All @@ -406,7 +406,7 @@ static int writePNG(const QRcode *qrcode, const char *outfile, enum imageType ty
}
}
/* bottom margin */
memset(row, 0xff, (realwidth + 7) / 8);
memset(row, 0xff, (size_t)((realwidth + 7) / 8));
for(y = 0; y < margin * size; y++) {
png_write_row(png_ptr, row);
}
Expand Down Expand Up @@ -655,7 +655,7 @@ static int writeXPM(const QRcode *qrcode, const char *outfile)
realwidth = (qrcode->width + margin * 2) * size;
realmargin = margin * size;

row = malloc(realwidth + 1);
row = malloc((size_t)realwidth + 1);
if (!row ) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
Expand All @@ -674,7 +674,7 @@ static int writeXPM(const QRcode *qrcode, const char *outfile)
fprintf(fp, "\"B c #%s\",\n", bg);

fputs("/* pixels */\n", fp);
memset(row, 'B', realwidth);
memset(row, 'B', (size_t)realwidth);
row[realwidth] = '\0';

for (y = 0; y < realmargin; y++) {
Expand Down Expand Up @@ -727,8 +727,8 @@ static void writeANSI_margin(FILE* fp, int realwidth,
{
int y;

strncpy(buffer, white, white_s);
memset(buffer + white_s, ' ', realwidth * 2);
strncpy(buffer, white, (size_t)white_s);
memset(buffer + white_s, ' ', (size_t)realwidth * 2);
strcpy(buffer + white_s + realwidth * 2, "\033[0m\n"); // reset to default colors
for(y = 0; y < margin; y++ ){
fputs(buffer, fp);
Expand Down Expand Up @@ -766,7 +766,7 @@ static int writeANSI(const QRcode *qrcode, const char *outfile)

realwidth = (qrcode->width + margin * 2) * size;
buffer_s = (realwidth * white_s) * 2;
buffer = (char *)malloc(buffer_s);
buffer = (char *)malloc((size_t)buffer_s);
if(buffer == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
Expand All @@ -780,8 +780,8 @@ static int writeANSI(const QRcode *qrcode, const char *outfile)
for(y = 0; y < qrcode->width; y++) {
row = (p+(y*qrcode->width));

memset(buffer, 0, buffer_s);
strncpy(buffer, white, white_s);
memset(buffer, 0, (size_t)buffer_s);
strncpy(buffer, white, (size_t)white_s);
for(x = 0; x < margin; x++ ){
strncat(buffer, " ", 2);
}
Expand All @@ -790,18 +790,18 @@ static int writeANSI(const QRcode *qrcode, const char *outfile)
for(x = 0; x < qrcode->width; x++) {
if(*(row+x)&0x1) {
if( last != 1 ){
strncat(buffer, black, black_s);
strncat(buffer, black, (size_t)black_s);
last = 1;
}
} else if( last != 0 ){
strncat(buffer, white, white_s);
strncat(buffer, white, (size_t)white_s);
last = 0;
}
strncat(buffer, " ", 2);
}

if( last != 0 ){
strncat(buffer, white, white_s);
strncat(buffer, white, (size_t)white_s);
}
for(x = 0; x < margin; x++ ){
strncat(buffer, " ", 2);
Expand Down Expand Up @@ -920,7 +920,7 @@ static void writeASCII_margin(FILE* fp, int realwidth, char* buffer, int invert)

h = margin;

memset(buffer, (invert?'#':' '), realwidth);
memset(buffer, (invert?'#':' '), (size_t)realwidth);
buffer[realwidth] = '\n';
buffer[realwidth + 1] = '\0';
for(y = 0; y < h; y++ ){
Expand Down Expand Up @@ -950,7 +950,7 @@ static int writeASCII(const QRcode *qrcode, const char *outfile, int invert)

realwidth = (qrcode->width + margin * 2) * 2;
buffer_s = realwidth + 2;
buffer = (char *)malloc( buffer_s );
buffer = (char *)malloc((size_t)buffer_s);
if(buffer == NULL) {
fprintf(stderr, "Failed to allocate memory.\n");
exit(EXIT_FAILURE);
Expand All @@ -964,7 +964,7 @@ static int writeASCII(const QRcode *qrcode, const char *outfile, int invert)
row = qrcode->data+(y*qrcode->width);
p = buffer;

memset(p, white, margin * 2);
memset(p, white, (size_t)margin * 2);
p += margin * 2;

for(x = 0; x < qrcode->width; x++) {
Expand All @@ -977,7 +977,7 @@ static int writeASCII(const QRcode *qrcode, const char *outfile, int invert)
}
}

memset(p, white, margin * 2);
memset(p, white, (size_t)margin * 2);
p += margin * 2;
*p++ = '\n';
*p++ = '\0';
Expand Down
16 changes: 8 additions & 8 deletions qrinput.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,21 +430,21 @@ static int QRinput_encodeModeNum(QRinput_List *entry, BitStream *bstream, int ve

words = entry->size / 3;
for(i = 0; i < words; i++) {
val = (entry->data[i*3 ] - '0') * 100;
val += (entry->data[i*3+1] - '0') * 10;
val += (entry->data[i*3+2] - '0');
val = (unsigned int)(entry->data[i*3 ] - '0') * 100;
val += (unsigned int)(entry->data[i*3+1] - '0') * 10;
val += (unsigned int)(entry->data[i*3+2] - '0');

ret = BitStream_appendNum(bstream, 10, val);
if(ret < 0) return -1;
}

if(entry->size - words * 3 == 1) {
val = entry->data[words*3] - '0';
val = (unsigned int)(entry->data[words*3] - '0');
ret = BitStream_appendNum(bstream, 4, val);
if(ret < 0) return -1;
} else if(entry->size - words * 3 == 2) {
val = (entry->data[words*3 ] - '0') * 10;
val += (entry->data[words*3+1] - '0');
val = (unsigned int)(entry->data[words*3 ] - '0') * 10;
val += (unsigned int)(entry->data[words*3+1] - '0');
BitStream_appendNum(bstream, 7, val);
if(ret < 0) return -1;
}
Expand Down Expand Up @@ -715,9 +715,9 @@ static int QRinput_encodeModeStructure(QRinput_List *entry, BitStream *bstream,

ret = BitStream_appendNum(bstream, 4, QRSPEC_MODEID_STRUCTURE);
if(ret < 0) return -1;
ret = BitStream_appendNum(bstream, 4, entry->data[1] - 1);
ret = BitStream_appendNum(bstream, 4, entry->data[1] - 1U);
if(ret < 0) return -1;
ret = BitStream_appendNum(bstream, 4, entry->data[0] - 1);
ret = BitStream_appendNum(bstream, 4, entry->data[0] - 1U);
if(ret < 0) return -1;
ret = BitStream_appendNum(bstream, 8, entry->data[2]);
if(ret < 0) return -1;
Expand Down
4 changes: 2 additions & 2 deletions rsecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ int RSECC_encode(size_t data_length, size_t ecc_length, const unsigned char *dat
feedback = aindex[data[i] ^ ecc[0]];
if(feedback != symbols) {
for(j = 1; j < ecc_length; j++) {
ecc[j] ^= alpha[(feedback + gen[ecc_length - j]) % symbols];
ecc[j] ^= alpha[(unsigned int)(feedback + gen[ecc_length - j]) % symbols];
}
}
memmove(&ecc[0], &ecc[1], ecc_length - 1);
if(feedback != symbols) {
ecc[ecc_length - 1] = alpha[(feedback + gen[0]) % symbols];
ecc[ecc_length - 1] = alpha[(unsigned int)(feedback + gen[0]) % symbols];
} else {
ecc[ecc_length - 1] = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int ncmpBin(char *correct, BitStream *bstream, size_t len)

int cmpBin(char *correct, BitStream *bstream)
{
int len = 0;
size_t len = 0;
char *p;


Expand Down
26 changes: 13 additions & 13 deletions tests/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static DataChunk *decodeNum(int *bits_length, unsigned char **bits, int version,
return NULL;
}

buf = (char *)malloc(size + 1);
buf = (char *)malloc((size_t)size + 1);
p = *bits;
q = buf;
for(i=0; i<words; i++) {
Expand Down Expand Up @@ -136,13 +136,13 @@ static DataChunk *decodeAn(int *bits_length, unsigned char **bits, int version,
return NULL;
}

buf = (char *)malloc(size + 1);
buf = (char *)malloc((size_t)size + 1);
p = *bits;
q = buf;
for(i=0; i<words; i++) {
val = bitToInt(p, 11);
ch = val / 45;
cl = val % 45;
ch = (int)(val / 45);
cl = (int)(val % 45);
sprintf(q, "%c%c", decodeAnTable[ch], decodeAnTable[cl]);
p += 11;
q += 2;
Expand Down Expand Up @@ -178,7 +178,7 @@ static DataChunk *decode8(int *bits_length, unsigned char **bits, int version, i
return NULL;
}

buf = (unsigned char *)malloc(size);
buf = (unsigned char *)malloc((size_t)size);
p = *bits;
q = buf;
for(i=0; i<size; i++) {
Expand All @@ -203,7 +203,7 @@ static DataChunk *decodeKanji(int *bits_length, unsigned char **bits, int versio
unsigned char *p;
char *buf, *q;
unsigned int val;
int ch, cl;
unsigned int ch, cl;
DataChunk *chunk;

size = decodeLength(bits_length, bits, QR_MODE_KANJI, version, mqr);
Expand All @@ -215,7 +215,7 @@ static DataChunk *decodeKanji(int *bits_length, unsigned char **bits, int versio
return NULL;
}

buf = (char *)malloc(size * 2 + 1);
buf = (char *)malloc((size_t)size * 2 + 1);
p = *bits;
q = buf;
for(i=0; i<size; i++) {
Expand Down Expand Up @@ -244,7 +244,7 @@ static DataChunk *decodeKanji(int *bits_length, unsigned char **bits, int versio

static DataChunk *decodeChunk(int *bits_length, unsigned char **bits, int version)
{
int val;
unsigned int val;

if(*bits_length < 4) {
return NULL;
Expand Down Expand Up @@ -434,7 +434,7 @@ int QRcode_decodeVersion(QRcode *code)
return -1;
}

return v1 >> 12;
return (int)(v1 >> 12);
}

int QRcode_decodeFormat(QRcode *code, QRecLevel *level, int *mask)
Expand Down Expand Up @@ -620,7 +620,7 @@ static BitStream *extractBits(int width, unsigned char *frame, int spec[5])
words = QRspec_rsDataLength(spec);
d1 = QRspec_rsDataCodes1(spec);
b1 = QRspec_rsBlockNum1(spec);
bits = (unsigned char *)malloc(words * 8);
bits = (unsigned char *)malloc((size_t)words * 8);
/*
* 00 01 02 03 04 05 06 07 08 09
* 10 11 12 13 14 15 16 17 18 19
Expand All @@ -643,7 +643,7 @@ static BitStream *extractBits(int width, unsigned char *frame, int spec[5])
}
free(filler);

bstream = BitStream_newWithBits(words * 8, bits);
bstream = BitStream_newWithBits((size_t)words * 8, bits);
free(bits);

return bstream;
Expand Down Expand Up @@ -833,14 +833,14 @@ static BitStream *extractBitsMQR(int width, unsigned char *frame, int version, Q
int size;

size = MQRspec_getDataLengthBit(version, level) + MQRspec_getECCLength(version, level) * 8;
bits = (unsigned char *)malloc(size);
bits = (unsigned char *)malloc((size_t)size);
filler = FrameFiller_new(width, frame, 1);
for(i=0; i<size; i++) {
bits[i] = *(FrameFiller_next(filler)) & 1;
}
free(filler);

bstream = BitStream_newWithBits(size, bits);
bstream = BitStream_newWithBits((size_t)size, bits);
free(bits);

return bstream;
Expand Down

0 comments on commit 0649403

Please sign in to comment.