Skip to content

Commit

Permalink
refactor: Use inttypes.h macros instead of casts to print fixed-width…
Browse files Browse the repository at this point in the history
… integers (#520)
  • Loading branch information
WillAyd authored Jun 12, 2024
1 parent 2fd50f7 commit f6a6b40
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 168 deletions.
5 changes: 3 additions & 2 deletions src/apps/dump_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "nanoarrow/nanoarrow_ipc.h"

#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
Expand Down Expand Up @@ -121,8 +122,8 @@ int main(int argc, char* argv[]) {

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

ArrowArrayStreamRelease(&stream);
fclose(file_ptr);
Expand Down
193 changes: 102 additions & 91 deletions src/nanoarrow/array.c

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/nanoarrow/nanoarrow_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

#include <errno.h>
#include <inttypes.h>

#include "nanoarrow.h"

Expand Down Expand Up @@ -365,8 +366,9 @@ ArrowErrorCode ArrowDeviceArrayViewSetArrayMinimal(
struct ArrowDevice* device =
ArrowDeviceResolve(device_array->device_type, device_array->device_id);
if (device == NULL) {
ArrowErrorSet(error, "Can't resolve device with type %d and identifier %ld",
(int)device_array->device_type, (long)device_array->device_id);
ArrowErrorSet(error,
"Can't resolve device with type %" PRId32 " and identifier %" PRId64,
device_array->device_type, device_array->device_id);
return EINVAL;
}

Expand Down
4 changes: 3 additions & 1 deletion src/nanoarrow/nanoarrow_device_cuda.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// specific language governing permissions and limitations
// under the License.

#include <inttypes.h>

#include <cuda.h>

#include "nanoarrow_device.h"
Expand Down Expand Up @@ -409,7 +411,7 @@ static ArrowErrorCode ArrowDeviceCudaInitDevice(struct ArrowDevice* device,
case ARROW_DEVICE_CUDA_HOST:
break;
default:
ArrowErrorSet(error, "Device type code %d not supported", (int)device_type);
ArrowErrorSet(error, "Device type code %" PRId32 " not supported", device_type);
return EINVAL;
}

Expand Down
91 changes: 49 additions & 42 deletions src/nanoarrow/nanoarrow_ipc_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>

Expand Down Expand Up @@ -300,8 +301,8 @@ static int ArrowIpcDecoderSetMetadata(struct ArrowSchema* schema,

if (n_pairs > 2147483647) {
ArrowErrorSet(error,
"Expected between 0 and 2147483647 key/value pairs but found %ld",
(long)n_pairs);
"Expected between 0 and 2147483647 key/value pairs but found %" PRId64,
n_pairs);
return EINVAL;
}

Expand Down Expand Up @@ -381,7 +382,7 @@ static int ArrowIpcDecoderSetTypeInt(struct ArrowSchema* schema,
default:
ArrowErrorSet(error,
"Expected signed int bitwidth of 8, 16, 32, or 64 but got %d",
(int)bitwidth);
bitwidth);
return EINVAL;
}
} else {
Expand All @@ -401,7 +402,7 @@ static int ArrowIpcDecoderSetTypeInt(struct ArrowSchema* schema,
default:
ArrowErrorSet(error,
"Expected unsigned int bitwidth of 8, 16, 32, or 64 but got %d",
(int)bitwidth);
bitwidth);
return EINVAL;
}
}
Expand All @@ -422,8 +423,7 @@ static int ArrowIpcDecoderSetTypeFloatingPoint(struct ArrowSchema* schema,
case ns(Precision_DOUBLE):
return ArrowIpcDecoderSetTypeSimple(schema, NANOARROW_TYPE_DOUBLE, error);
default:
ArrowErrorSet(error, "Unexpected FloatingPoint Precision value: %d",
(int)precision);
ArrowErrorSet(error, "Unexpected FloatingPoint Precision value: %d", precision);
return EINVAL;
}
}
Expand All @@ -447,7 +447,7 @@ static int ArrowIpcDecoderSetTypeDecimal(struct ArrowSchema* schema,
ArrowSchemaSetTypeDecimal(schema, NANOARROW_TYPE_DECIMAL256, precision, scale);
break;
default:
ArrowErrorSet(error, "Unexpected Decimal bitwidth value: %d", (int)bitwidth);
ArrowErrorSet(error, "Unexpected Decimal bitwidth value: %d", bitwidth);
return EINVAL;
}

Expand Down Expand Up @@ -518,7 +518,7 @@ static int ArrowIpcDecoderSetTypeTime(struct ArrowSchema* schema,
break;

default:
ArrowErrorSet(error, "Unexpected Time TimeUnit value: %d", (int)time_unit);
ArrowErrorSet(error, "Unexpected Time TimeUnit value: %d", time_unit);
return EINVAL;
}

Expand Down Expand Up @@ -584,7 +584,7 @@ static int ArrowIpcDecoderSetTypeInterval(struct ArrowSchema* schema,
return ArrowIpcDecoderSetTypeSimple(schema, NANOARROW_TYPE_INTERVAL_MONTH_DAY_NANO,
error);
default:
ArrowErrorSet(error, "Unexpected Interval unit value: %d", (int)interval_unit);
ArrowErrorSet(error, "Unexpected Interval unit value: %d", interval_unit);
return EINVAL;
}
}
Expand Down Expand Up @@ -645,8 +645,8 @@ static int ArrowIpcDecoderSetTypeUnion(struct ArrowSchema* schema,

if (n_children < 0 || n_children > 127) {
ArrowErrorSet(error,
"Expected between 0 and 127 children for Union type but found %ld",
(long)n_children);
"Expected between 0 and 127 children for Union type but found %" PRId64,
n_children);
return EINVAL;
}

Expand All @@ -672,7 +672,7 @@ static int ArrowIpcDecoderSetTypeUnion(struct ArrowSchema* schema,
format_out_size -= n_chars;
break;
default:
ArrowErrorSet(error, "Unexpected Union UnionMode value: %d", (int)union_mode);
ArrowErrorSet(error, "Unexpected Union UnionMode value: %d", union_mode);
return EINVAL;
}

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

if (n_type_ids != n_children) {
ArrowErrorSet(
error,
"Expected between %ld children for Union type with %ld typeIds but found %ld",
(long)n_type_ids, (long)n_type_ids, (long)n_children);
ArrowErrorSet(error,
"Expected between %" PRId64 " children for Union type with %" PRId64
" typeIds but found %" PRId64,
n_type_ids, n_type_ids, n_children);
return EINVAL;
}

Expand All @@ -705,8 +705,8 @@ static int ArrowIpcDecoderSetTypeUnion(struct ArrowSchema* schema,
}

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

Expand All @@ -727,7 +727,7 @@ static int ArrowIpcDecoderSetTypeUnion(struct ArrowSchema* schema,
}

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

Expand Down Expand Up @@ -792,7 +792,7 @@ static int ArrowIpcDecoderSetType(struct ArrowSchema* schema, ns(Field_table_t)
return ArrowIpcDecoderSetTypeUnion(schema, ns(Field_type_get(field)), n_children,
error);
default:
ArrowErrorSet(error, "Unrecognized Field type with value %d", (int)type_type);
ArrowErrorSet(error, "Unrecognized Field type with value %d", type_type);
return EINVAL;
}
}
Expand Down Expand Up @@ -878,7 +878,7 @@ static int ArrowIpcDecoderDecodeSchemaHeader(struct ArrowIpcDecoder* decoder,
default:
ArrowErrorSet(error,
"Expected Schema endianness of 0 (little) or 1 (big) but got %d",
(int)endianness);
endianness);
return EINVAL;
}

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

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

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

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

Expand All @@ -997,9 +998,10 @@ static inline int ArrowIpcDecoderReadHeaderPrefix(struct ArrowIpcDecoder* decode
int32_t header_body_size_bytes = ArrowIpcReadInt32LE(data_mut, swap_endian);
*message_size_bytes = header_body_size_bytes + kMessageHeaderPrefixSize;
if (header_body_size_bytes < 0) {
ArrowErrorSet(
error, "Expected message body size > 0 but found message body size of %ld bytes",
(long)header_body_size_bytes);
ArrowErrorSet(error,
"Expected message body size > 0 but found message body size of %" PRId32
" bytes",
header_body_size_bytes);
return EINVAL;
}

Expand Down Expand Up @@ -1035,9 +1037,10 @@ ArrowErrorCode ArrowIpcDecoderVerifyHeader(struct ArrowIpcDecoder* decoder,
int64_t message_body_size = decoder->header_size_bytes - kMessageHeaderPrefixSize;
if (data.size_bytes < message_body_size) {
ArrowErrorSet(error,
"Expected >= %ld bytes of remaining data but found %ld bytes in buffer",
(long)message_body_size + kMessageHeaderPrefixSize,
(long)data.size_bytes + kMessageHeaderPrefixSize);
"Expected >= %" PRId64 " bytes of remaining data but found %" PRId64
" bytes in buffer",
message_body_size + kMessageHeaderPrefixSize,
data.size_bytes + kMessageHeaderPrefixSize);
return ESPIPE;
}

Expand Down Expand Up @@ -1073,9 +1076,10 @@ ArrowErrorCode ArrowIpcDecoderDecodeHeader(struct ArrowIpcDecoder* decoder,
int64_t message_body_size = decoder->header_size_bytes - kMessageHeaderPrefixSize;
if (data.size_bytes < message_body_size) {
ArrowErrorSet(error,
"Expected >= %ld bytes of remaining data but found %ld bytes in buffer",
(long)message_body_size + kMessageHeaderPrefixSize,
(long)data.size_bytes + kMessageHeaderPrefixSize);
"Expected >= %" PRId64 " bytes of remaining data but found %" PRId64
" bytes in buffer",
message_body_size + kMessageHeaderPrefixSize,
data.size_bytes + kMessageHeaderPrefixSize);
return ESPIPE;
}

Expand Down Expand Up @@ -1152,8 +1156,8 @@ ArrowErrorCode ArrowIpcDecoderDecodeSchema(struct ArrowIpcDecoder* decoder,
int result = ArrowSchemaSetTypeStruct(&tmp, n_fields);
if (result != NANOARROW_OK) {
ArrowSchemaRelease(&tmp);
ArrowErrorSet(error, "Failed to allocate struct schema with %ld children",
(long)n_fields);
ArrowErrorSet(error, "Failed to allocate struct schema with %" PRId64 " children",
n_fields);
return result;
}

Expand Down Expand Up @@ -1462,8 +1466,9 @@ static int ArrowIpcDecoderSwapEndian(struct ArrowIpcBufferSource* src,
break;
}
default:
ArrowErrorSet(error, "Endian swapping for element bitwidth %d is not supported",
(int)src->element_size_bits);
ArrowErrorSet(
error, "Endian swapping for element bitwidth %" PRId64 " is not supported",
src->element_size_bits);
return ENOTSUP;
}
break;
Expand Down Expand Up @@ -1498,8 +1503,10 @@ static int ArrowIpcDecoderMakeBuffer(struct ArrowIpcArraySetter* setter, int64_t
int64_t buffer_start = offset;
int64_t buffer_end = buffer_start + length;
if (buffer_start < 0 || buffer_end > setter->body_size_bytes) {
ArrowErrorSet(error, "Buffer requires body offsets [%ld..%ld) but body has size %ld",
(long)buffer_start, (long)buffer_end, (long)setter->body_size_bytes);
ArrowErrorSet(error,
"Buffer requires body offsets [%" PRId64 "..%" PRId64
") but body has size %" PRId64,
buffer_start, buffer_end, setter->body_size_bytes);
return EINVAL;
}

Expand Down
6 changes: 4 additions & 2 deletions src/nanoarrow/nanoarrow_ipc_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>

Expand Down Expand Up @@ -283,8 +284,9 @@ static int ArrowIpcArrayStreamReaderNextBody(

if (bytes_read != bytes_to_read) {
ArrowErrorSet(&private_data->error,
"Expected to be able to read %ld bytes for message body but got %ld",
(long)bytes_to_read, bytes_read);
"Expected to be able to read %" PRId64
" bytes for message body but got %" PRId64,
bytes_to_read, bytes_read);
return ESPIPE;
} else {
return NANOARROW_OK;
Expand Down
Loading

0 comments on commit f6a6b40

Please sign in to comment.