Skip to content

Commit f6a6b40

Browse files
authored
refactor: Use inttypes.h macros instead of casts to print fixed-width integers (#520)
1 parent 2fd50f7 commit f6a6b40

File tree

7 files changed

+195
-168
lines changed

7 files changed

+195
-168
lines changed

src/apps/dump_stream.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "nanoarrow/nanoarrow_ipc.h"
1919

20+
#include <inttypes.h>
2021
#include <stdio.h>
2122
#include <string.h>
2223
#include <time.h>
@@ -121,8 +122,8 @@ int main(int argc, char* argv[]) {
121122

122123
end = clock();
123124
elapsed = (end - begin) / ((double)CLOCKS_PER_SEC);
124-
fprintf(stdout, "Read %ld rows in %ld batch(es) <%.06f seconds>\n", (long)row_count,
125-
(long)batch_count, elapsed);
125+
fprintf(stdout, "Read %l" PRId64 " rows in %" PRId64 " batch(es) <%.06f seconds>\n",
126+
row_count, batch_count, elapsed);
126127

127128
ArrowArrayStreamRelease(&stream);
128129
fclose(file_ptr);

src/nanoarrow/array.c

Lines changed: 102 additions & 91 deletions
Large diffs are not rendered by default.

src/nanoarrow/nanoarrow_device.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
#include <errno.h>
19+
#include <inttypes.h>
1920

2021
#include "nanoarrow.h"
2122

@@ -365,8 +366,9 @@ ArrowErrorCode ArrowDeviceArrayViewSetArrayMinimal(
365366
struct ArrowDevice* device =
366367
ArrowDeviceResolve(device_array->device_type, device_array->device_id);
367368
if (device == NULL) {
368-
ArrowErrorSet(error, "Can't resolve device with type %d and identifier %ld",
369-
(int)device_array->device_type, (long)device_array->device_id);
369+
ArrowErrorSet(error,
370+
"Can't resolve device with type %" PRId32 " and identifier %" PRId64,
371+
device_array->device_type, device_array->device_id);
370372
return EINVAL;
371373
}
372374

src/nanoarrow/nanoarrow_device_cuda.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
#include <inttypes.h>
19+
1820
#include <cuda.h>
1921

2022
#include "nanoarrow_device.h"
@@ -409,7 +411,7 @@ static ArrowErrorCode ArrowDeviceCudaInitDevice(struct ArrowDevice* device,
409411
case ARROW_DEVICE_CUDA_HOST:
410412
break;
411413
default:
412-
ArrowErrorSet(error, "Device type code %d not supported", (int)device_type);
414+
ArrowErrorSet(error, "Device type code %" PRId32 " not supported", device_type);
413415
return EINVAL;
414416
}
415417

src/nanoarrow/nanoarrow_ipc_decoder.c

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
#include <errno.h>
19+
#include <inttypes.h>
1920
#include <stdio.h>
2021
#include <string.h>
2122

@@ -300,8 +301,8 @@ static int ArrowIpcDecoderSetMetadata(struct ArrowSchema* schema,
300301

301302
if (n_pairs > 2147483647) {
302303
ArrowErrorSet(error,
303-
"Expected between 0 and 2147483647 key/value pairs but found %ld",
304-
(long)n_pairs);
304+
"Expected between 0 and 2147483647 key/value pairs but found %" PRId64,
305+
n_pairs);
305306
return EINVAL;
306307
}
307308

@@ -381,7 +382,7 @@ static int ArrowIpcDecoderSetTypeInt(struct ArrowSchema* schema,
381382
default:
382383
ArrowErrorSet(error,
383384
"Expected signed int bitwidth of 8, 16, 32, or 64 but got %d",
384-
(int)bitwidth);
385+
bitwidth);
385386
return EINVAL;
386387
}
387388
} else {
@@ -401,7 +402,7 @@ static int ArrowIpcDecoderSetTypeInt(struct ArrowSchema* schema,
401402
default:
402403
ArrowErrorSet(error,
403404
"Expected unsigned int bitwidth of 8, 16, 32, or 64 but got %d",
404-
(int)bitwidth);
405+
bitwidth);
405406
return EINVAL;
406407
}
407408
}
@@ -422,8 +423,7 @@ static int ArrowIpcDecoderSetTypeFloatingPoint(struct ArrowSchema* schema,
422423
case ns(Precision_DOUBLE):
423424
return ArrowIpcDecoderSetTypeSimple(schema, NANOARROW_TYPE_DOUBLE, error);
424425
default:
425-
ArrowErrorSet(error, "Unexpected FloatingPoint Precision value: %d",
426-
(int)precision);
426+
ArrowErrorSet(error, "Unexpected FloatingPoint Precision value: %d", precision);
427427
return EINVAL;
428428
}
429429
}
@@ -447,7 +447,7 @@ static int ArrowIpcDecoderSetTypeDecimal(struct ArrowSchema* schema,
447447
ArrowSchemaSetTypeDecimal(schema, NANOARROW_TYPE_DECIMAL256, precision, scale);
448448
break;
449449
default:
450-
ArrowErrorSet(error, "Unexpected Decimal bitwidth value: %d", (int)bitwidth);
450+
ArrowErrorSet(error, "Unexpected Decimal bitwidth value: %d", bitwidth);
451451
return EINVAL;
452452
}
453453

@@ -518,7 +518,7 @@ static int ArrowIpcDecoderSetTypeTime(struct ArrowSchema* schema,
518518
break;
519519

520520
default:
521-
ArrowErrorSet(error, "Unexpected Time TimeUnit value: %d", (int)time_unit);
521+
ArrowErrorSet(error, "Unexpected Time TimeUnit value: %d", time_unit);
522522
return EINVAL;
523523
}
524524

@@ -584,7 +584,7 @@ static int ArrowIpcDecoderSetTypeInterval(struct ArrowSchema* schema,
584584
return ArrowIpcDecoderSetTypeSimple(schema, NANOARROW_TYPE_INTERVAL_MONTH_DAY_NANO,
585585
error);
586586
default:
587-
ArrowErrorSet(error, "Unexpected Interval unit value: %d", (int)interval_unit);
587+
ArrowErrorSet(error, "Unexpected Interval unit value: %d", interval_unit);
588588
return EINVAL;
589589
}
590590
}
@@ -645,8 +645,8 @@ static int ArrowIpcDecoderSetTypeUnion(struct ArrowSchema* schema,
645645

646646
if (n_children < 0 || n_children > 127) {
647647
ArrowErrorSet(error,
648-
"Expected between 0 and 127 children for Union type but found %ld",
649-
(long)n_children);
648+
"Expected between 0 and 127 children for Union type but found %" PRId64,
649+
n_children);
650650
return EINVAL;
651651
}
652652

@@ -672,7 +672,7 @@ static int ArrowIpcDecoderSetTypeUnion(struct ArrowSchema* schema,
672672
format_out_size -= n_chars;
673673
break;
674674
default:
675-
ArrowErrorSet(error, "Unexpected Union UnionMode value: %d", (int)union_mode);
675+
ArrowErrorSet(error, "Unexpected Union UnionMode value: %d", union_mode);
676676
return EINVAL;
677677
}
678678

@@ -686,10 +686,10 @@ static int ArrowIpcDecoderSetTypeUnion(struct ArrowSchema* schema,
686686
int64_t n_type_ids = flatbuffers_int32_vec_len(type_ids);
687687

688688
if (n_type_ids != n_children) {
689-
ArrowErrorSet(
690-
error,
691-
"Expected between %ld children for Union type with %ld typeIds but found %ld",
692-
(long)n_type_ids, (long)n_type_ids, (long)n_children);
689+
ArrowErrorSet(error,
690+
"Expected between %" PRId64 " children for Union type with %" PRId64
691+
" typeIds but found %" PRId64,
692+
n_type_ids, n_type_ids, n_children);
693693
return EINVAL;
694694
}
695695

@@ -705,8 +705,8 @@ static int ArrowIpcDecoderSetTypeUnion(struct ArrowSchema* schema,
705705
}
706706

707707
for (int64_t i = 1; i < n_type_ids; i++) {
708-
n_chars = snprintf(format_cursor, format_out_size, ",%d",
709-
(int)flatbuffers_int32_vec_at(type_ids, i));
708+
n_chars = snprintf(format_cursor, format_out_size, ",%" PRId32,
709+
flatbuffers_int32_vec_at(type_ids, i));
710710
format_cursor += n_chars;
711711
format_out_size -= n_chars;
712712

@@ -727,7 +727,7 @@ static int ArrowIpcDecoderSetTypeUnion(struct ArrowSchema* schema,
727727
}
728728

729729
for (int64_t i = 1; i < n_children; i++) {
730-
n_chars = snprintf(format_cursor, format_out_size, ",%d", (int)i);
730+
n_chars = snprintf(format_cursor, format_out_size, ",%" PRId64, i);
731731
format_cursor += n_chars;
732732
format_out_size -= n_chars;
733733

@@ -792,7 +792,7 @@ static int ArrowIpcDecoderSetType(struct ArrowSchema* schema, ns(Field_table_t)
792792
return ArrowIpcDecoderSetTypeUnion(schema, ns(Field_type_get(field)), n_children,
793793
error);
794794
default:
795-
ArrowErrorSet(error, "Unrecognized Field type with value %d", (int)type_type);
795+
ArrowErrorSet(error, "Unrecognized Field type with value %d", type_type);
796796
return EINVAL;
797797
}
798798
}
@@ -878,7 +878,7 @@ static int ArrowIpcDecoderDecodeSchemaHeader(struct ArrowIpcDecoder* decoder,
878878
default:
879879
ArrowErrorSet(error,
880880
"Expected Schema endianness of 0 (little) or 1 (big) but got %d",
881-
(int)endianness);
881+
endianness);
882882
return EINVAL;
883883
}
884884

@@ -920,14 +920,14 @@ static int ArrowIpcDecoderDecodeRecordBatchHeader(struct ArrowIpcDecoder* decode
920920
// Check field node and buffer count. We have one more field and buffer
921921
// because we count the root struct and the flatbuffer message does not.
922922
if ((n_fields + 1) != private_data->n_fields) {
923-
ArrowErrorSet(error, "Expected %ld field nodes in message but found %ld",
924-
(long)private_data->n_fields - 1, (long)n_fields);
923+
ArrowErrorSet(error, "Expected %" PRId64 " field nodes in message but found %" PRId64,
924+
private_data->n_fields - 1, n_fields);
925925
return EINVAL;
926926
}
927927

928928
if ((n_buffers + 1) != private_data->n_buffers) {
929-
ArrowErrorSet(error, "Expected %ld buffers in message but found %ld",
930-
(long)private_data->n_buffers - 1, (long)n_buffers);
929+
ArrowErrorSet(error, "Expected %" PRId64 " buffers in message but found %" PRId64,
930+
private_data->n_buffers - 1, n_buffers);
931931
return EINVAL;
932932
}
933933

@@ -981,8 +981,9 @@ static inline int ArrowIpcDecoderReadHeaderPrefix(struct ArrowIpcDecoder* decode
981981
(struct ArrowIpcDecoderPrivate*)decoder->private_data;
982982

983983
if (data_mut->size_bytes < kMessageHeaderPrefixSize) {
984-
ArrowErrorSet(error, "Expected data of at least 8 bytes but only %ld bytes remain",
985-
(long)data_mut->size_bytes);
984+
ArrowErrorSet(error,
985+
"Expected data of at least 8 bytes but only %" PRId64 " bytes remain",
986+
data_mut->size_bytes);
986987
return ESPIPE;
987988
}
988989

@@ -997,9 +998,10 @@ static inline int ArrowIpcDecoderReadHeaderPrefix(struct ArrowIpcDecoder* decode
997998
int32_t header_body_size_bytes = ArrowIpcReadInt32LE(data_mut, swap_endian);
998999
*message_size_bytes = header_body_size_bytes + kMessageHeaderPrefixSize;
9991000
if (header_body_size_bytes < 0) {
1000-
ArrowErrorSet(
1001-
error, "Expected message body size > 0 but found message body size of %ld bytes",
1002-
(long)header_body_size_bytes);
1001+
ArrowErrorSet(error,
1002+
"Expected message body size > 0 but found message body size of %" PRId32
1003+
" bytes",
1004+
header_body_size_bytes);
10031005
return EINVAL;
10041006
}
10051007

@@ -1035,9 +1037,10 @@ ArrowErrorCode ArrowIpcDecoderVerifyHeader(struct ArrowIpcDecoder* decoder,
10351037
int64_t message_body_size = decoder->header_size_bytes - kMessageHeaderPrefixSize;
10361038
if (data.size_bytes < message_body_size) {
10371039
ArrowErrorSet(error,
1038-
"Expected >= %ld bytes of remaining data but found %ld bytes in buffer",
1039-
(long)message_body_size + kMessageHeaderPrefixSize,
1040-
(long)data.size_bytes + kMessageHeaderPrefixSize);
1040+
"Expected >= %" PRId64 " bytes of remaining data but found %" PRId64
1041+
" bytes in buffer",
1042+
message_body_size + kMessageHeaderPrefixSize,
1043+
data.size_bytes + kMessageHeaderPrefixSize);
10411044
return ESPIPE;
10421045
}
10431046

@@ -1073,9 +1076,10 @@ ArrowErrorCode ArrowIpcDecoderDecodeHeader(struct ArrowIpcDecoder* decoder,
10731076
int64_t message_body_size = decoder->header_size_bytes - kMessageHeaderPrefixSize;
10741077
if (data.size_bytes < message_body_size) {
10751078
ArrowErrorSet(error,
1076-
"Expected >= %ld bytes of remaining data but found %ld bytes in buffer",
1077-
(long)message_body_size + kMessageHeaderPrefixSize,
1078-
(long)data.size_bytes + kMessageHeaderPrefixSize);
1079+
"Expected >= %" PRId64 " bytes of remaining data but found %" PRId64
1080+
" bytes in buffer",
1081+
message_body_size + kMessageHeaderPrefixSize,
1082+
data.size_bytes + kMessageHeaderPrefixSize);
10791083
return ESPIPE;
10801084
}
10811085

@@ -1152,8 +1156,8 @@ ArrowErrorCode ArrowIpcDecoderDecodeSchema(struct ArrowIpcDecoder* decoder,
11521156
int result = ArrowSchemaSetTypeStruct(&tmp, n_fields);
11531157
if (result != NANOARROW_OK) {
11541158
ArrowSchemaRelease(&tmp);
1155-
ArrowErrorSet(error, "Failed to allocate struct schema with %ld children",
1156-
(long)n_fields);
1159+
ArrowErrorSet(error, "Failed to allocate struct schema with %" PRId64 " children",
1160+
n_fields);
11571161
return result;
11581162
}
11591163

@@ -1462,8 +1466,9 @@ static int ArrowIpcDecoderSwapEndian(struct ArrowIpcBufferSource* src,
14621466
break;
14631467
}
14641468
default:
1465-
ArrowErrorSet(error, "Endian swapping for element bitwidth %d is not supported",
1466-
(int)src->element_size_bits);
1469+
ArrowErrorSet(
1470+
error, "Endian swapping for element bitwidth %" PRId64 " is not supported",
1471+
src->element_size_bits);
14671472
return ENOTSUP;
14681473
}
14691474
break;
@@ -1498,8 +1503,10 @@ static int ArrowIpcDecoderMakeBuffer(struct ArrowIpcArraySetter* setter, int64_t
14981503
int64_t buffer_start = offset;
14991504
int64_t buffer_end = buffer_start + length;
15001505
if (buffer_start < 0 || buffer_end > setter->body_size_bytes) {
1501-
ArrowErrorSet(error, "Buffer requires body offsets [%ld..%ld) but body has size %ld",
1502-
(long)buffer_start, (long)buffer_end, (long)setter->body_size_bytes);
1506+
ArrowErrorSet(error,
1507+
"Buffer requires body offsets [%" PRId64 "..%" PRId64
1508+
") but body has size %" PRId64,
1509+
buffer_start, buffer_end, setter->body_size_bytes);
15031510
return EINVAL;
15041511
}
15051512

src/nanoarrow/nanoarrow_ipc_reader.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
#include <errno.h>
19+
#include <inttypes.h>
1920
#include <stdio.h>
2021
#include <string.h>
2122

@@ -283,8 +284,9 @@ static int ArrowIpcArrayStreamReaderNextBody(
283284

284285
if (bytes_read != bytes_to_read) {
285286
ArrowErrorSet(&private_data->error,
286-
"Expected to be able to read %ld bytes for message body but got %ld",
287-
(long)bytes_to_read, bytes_read);
287+
"Expected to be able to read %" PRId64
288+
" bytes for message body but got %" PRId64,
289+
bytes_to_read, bytes_read);
288290
return ESPIPE;
289291
} else {
290292
return NANOARROW_OK;

0 commit comments

Comments
 (0)