Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/nanoarrow/common/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ ArrowErrorCode ArrowArrayAllocateChildren(struct ArrowArray* array, int64_t n_ch
return ENOMEM;
}

memset(array->children, 0, n_children * sizeof(struct ArrowArray*));
memset(array->children, 0, _ArrowInt64ToSizeT(n_children) * sizeof(struct ArrowArray*));

for (int64_t i = 0; i < n_children; i++) {
array->children[i] = (struct ArrowArray*)ArrowMalloc(sizeof(struct ArrowArray));
Expand Down Expand Up @@ -2363,8 +2363,9 @@ static void ArrowArrayViewCompareBuffer(const struct ArrowArrayView* actual,
SET_NOT_EQUAL_AND_RETURN_IF(
actual->buffer_views[i].size_bytes != expected->buffer_views[i].size_bytes, state);

int64_t buffer_size = actual->buffer_views[i].size_bytes;
if (buffer_size > 0) {
int64_t size_bytes = actual->buffer_views[i].size_bytes;
if (size_bytes > 0) {
size_t buffer_size = _ArrowInt64ToSizeT(size_bytes);
SET_NOT_EQUAL_AND_RETURN_IF(
memcmp(actual->buffer_views[i].data.data, expected->buffer_views[i].data.data,
buffer_size) != 0,
Expand Down
3 changes: 2 additions & 1 deletion src/nanoarrow/common/inline_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,8 @@ static inline ArrowErrorCode ArrowArrayAppendBytes(struct ArrowArray* array,

if (value.size_bytes <= NANOARROW_BINARY_VIEW_INLINE_SIZE) {
if (value.size_bytes > 0) {
memcpy(bvt.inlined.data, value.data.as_char, value.size_bytes);
memcpy(bvt.inlined.data, value.data.as_char,
_ArrowInt64ToSizeT(value.size_bytes));
}
memset(bvt.inlined.data + bvt.inlined.size, 0,
NANOARROW_BINARY_VIEW_INLINE_SIZE - bvt.inlined.size);
Expand Down
4 changes: 2 additions & 2 deletions src/nanoarrow/common/inline_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ static inline void ArrowBufferAppendUnsafe(struct ArrowBuffer* buffer, const voi
int64_t size_bytes) {
if (size_bytes > 0) {
NANOARROW_DCHECK(buffer->data != NULL);
memcpy(buffer->data + buffer->size_bytes, data, size_bytes);
memcpy(buffer->data + buffer->size_bytes, data, _ArrowInt64ToSizeT(size_bytes));
buffer->size_bytes += size_bytes;
}
}
Expand Down Expand Up @@ -329,7 +329,7 @@ static inline ArrowErrorCode ArrowBufferAppendFill(struct ArrowBuffer* buffer,
NANOARROW_RETURN_NOT_OK(ArrowBufferReserve(buffer, size_bytes));

NANOARROW_DCHECK(buffer->data != NULL); // To help clang-tidy
memset(buffer->data + buffer->size_bytes, value, size_bytes);
memset(buffer->data + buffer->size_bytes, value, _ArrowInt64ToSizeT(size_bytes));
buffer->size_bytes += size_bytes;

return NANOARROW_OK;
Expand Down
18 changes: 17 additions & 1 deletion src/nanoarrow/common/inline_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ static inline void ArrowErrorSetString(struct ArrowError* error, const char* src
memcpy(error->message, src, sizeof(error->message) - 1);
error->message[sizeof(error->message) - 1] = '\0';
} else {
memcpy(error->message, src, src_len);
memcpy(error->message, src, (size_t)src_len);
error->message[src_len] = '\0';
}
}
Expand Down Expand Up @@ -327,6 +327,22 @@ static inline void ArrowErrorSetString(struct ArrowError* error, const char* src
#define NANOARROW_DCHECK(EXPR)
#endif

// Returns nonzero if x is a valid (non-negative) size_t value on this platform
static inline int _ArrowInt64FitsInSizeT(int64_t x) {
#if SIZE_MAX < INT64_MAX
return x >= 0 && x <= (int64_t)SIZE_MAX;
#else
return x >= 0;
#endif
}

// Convert a size or offset known to fit in size_t (e.g., the size of an existing
// in-memory allocation). Checked in debug builds only.
static inline size_t _ArrowInt64ToSizeT(int64_t x) {
NANOARROW_DCHECK(_ArrowInt64FitsInSizeT(x));
return (size_t)x;
}

static inline void ArrowSchemaMove(struct ArrowSchema* src, struct ArrowSchema* dst) {
NANOARROW_DCHECK(src != NULL);
NANOARROW_DCHECK(dst != NULL);
Expand Down
21 changes: 12 additions & 9 deletions src/nanoarrow/common/schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,18 +434,18 @@ ArrowErrorCode ArrowSchemaSetTypeUnion(struct ArrowSchema* schema, enum ArrowTyp
// Max valid size would be +ud:0,1,...126 = 401 characters + null terminator
char format_out[512];
int64_t format_out_size = 512;
memset(format_out, 0, format_out_size);
memset(format_out, 0, _ArrowInt64ToSizeT(format_out_size));
int n_chars;
char* format_cursor = format_out;

switch (type) {
case NANOARROW_TYPE_SPARSE_UNION:
n_chars = snprintf(format_cursor, format_out_size, "+us:");
n_chars = snprintf(format_cursor, _ArrowInt64ToSizeT(format_out_size), "+us:");
format_cursor += n_chars;
format_out_size -= n_chars;
break;
case NANOARROW_TYPE_DENSE_UNION:
n_chars = snprintf(format_cursor, format_out_size, "+ud:");
n_chars = snprintf(format_cursor, _ArrowInt64ToSizeT(format_out_size), "+ud:");
format_cursor += n_chars;
format_out_size -= n_chars;
break;
Expand All @@ -460,12 +460,13 @@ ArrowErrorCode ArrowSchemaSetTypeUnion(struct ArrowSchema* schema, enum ArrowTyp
}

if (n_children > 0) {
n_chars = snprintf(format_cursor, format_out_size, "0");
n_chars = snprintf(format_cursor, _ArrowInt64ToSizeT(format_out_size), "0");
format_cursor += n_chars;
format_out_size -= n_chars;

for (int64_t i = 1; i < n_children; i++) {
n_chars = snprintf(format_cursor, format_out_size, ",%" PRId64, i);
n_chars =
snprintf(format_cursor, _ArrowInt64ToSizeT(format_out_size), ",%" PRId64, i);
format_cursor += n_chars;
format_out_size -= n_chars;
}
Expand Down Expand Up @@ -533,7 +534,7 @@ ArrowErrorCode ArrowSchemaSetMetadata(struct ArrowSchema* schema, const char* me
}

if (metadata != NULL) {
size_t metadata_size = ArrowMetadataSizeOf(metadata);
size_t metadata_size = _ArrowInt64ToSizeT(ArrowMetadataSizeOf(metadata));
schema->metadata = (const char*)ArrowMalloc(metadata_size);
if (schema->metadata == NULL) {
return ENOMEM;
Expand Down Expand Up @@ -563,7 +564,8 @@ ArrowErrorCode ArrowSchemaAllocateChildren(struct ArrowSchema* schema,

schema->n_children = n_children;

memset(schema->children, 0, n_children * sizeof(struct ArrowSchema*));
memset(schema->children, 0,
_ArrowInt64ToSizeT(n_children) * sizeof(struct ArrowSchema*));

for (int64_t i = 0; i < n_children; i++) {
schema->children[i] = (struct ArrowSchema*)ArrowMalloc(sizeof(struct ArrowSchema));
Expand Down Expand Up @@ -1617,7 +1619,8 @@ static ArrowErrorCode ArrowMetadataGetValueInternal(const char* metadata,
while (ArrowMetadataReaderRead(&reader, &existing_key, &existing_value) ==
NANOARROW_OK) {
int key_equal = key->size_bytes == existing_key.size_bytes &&
strncmp(key->data, existing_key.data, existing_key.size_bytes) == 0;
strncmp(key->data, existing_key.data,
_ArrowInt64ToSizeT(existing_key.size_bytes)) == 0;
if (key_equal) {
value_out->data = existing_value.data;
value_out->size_bytes = existing_value.size_bytes;
Expand Down Expand Up @@ -1721,7 +1724,7 @@ static ArrowErrorCode ArrowMetadataBuilderSetInternal(struct ArrowBuffer* buffer

if (key->size_bytes == existing_key.size_bytes &&
strncmp((const char*)key->data, (const char*)existing_key.data,
existing_key.size_bytes) == 0) {
_ArrowInt64ToSizeT(existing_key.size_bytes)) == 0) {
result = ArrowMetadataBuilderAppendInternal(&new_buffer, key, value);
value = NULL;
} else {
Expand Down
27 changes: 21 additions & 6 deletions src/nanoarrow/common/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,22 @@ void ArrowLayoutInit(struct ArrowLayout* layout, enum ArrowType storage_type) {
}
}

void* ArrowMalloc(int64_t size) { return malloc(size); }
void* ArrowMalloc(int64_t size) {
if (!_ArrowInt64FitsInSizeT(size)) {
return NULL;
}

return malloc((size_t)size);
}

void* ArrowRealloc(void* ptr, int64_t size) { return realloc(ptr, size); }
void* ArrowRealloc(void* ptr, int64_t size) {
if (!_ArrowInt64FitsInSizeT(size)) {
free(ptr);
return NULL;
}

return realloc(ptr, (size_t)size);
}

void ArrowFree(void* ptr) { free(ptr); }

Expand Down Expand Up @@ -542,7 +555,7 @@ static void ShiftAndAdd(struct ArrowStringView value, uint32_t* out, int64_t out

const uint64_t multiple = kUInt32PowersOfTen[group_size];

memcpy(chunk_string, value.data + posn, group_size);
memcpy(chunk_string, value.data + posn, _ArrowInt64ToSizeT(group_size));
chunk_string[group_size] = '\0';
uint32_t chunk = (uint32_t)strtoll(chunk_string, NULL, 10);

Expand Down Expand Up @@ -627,8 +640,9 @@ ArrowErrorCode ArrowDecimalAppendDigitsToBuffer(const struct ArrowDecimal* decim
int32_t value;
memcpy(&value, decimal->words, sizeof(int32_t));
NANOARROW_RETURN_NOT_OK(ArrowBufferReserve(buffer, 16));
int n_chars = snprintf((char*)buffer->data + buffer->size_bytes,
(buffer->capacity_bytes - buffer->size_bytes), "%d", value);
int n_chars = snprintf(
(char*)buffer->data + buffer->size_bytes,
_ArrowInt64ToSizeT(buffer->capacity_bytes - buffer->size_bytes), "%d", value);
if (n_chars <= 0) {
return EINVAL;
}
Expand Down Expand Up @@ -783,7 +797,8 @@ ArrowErrorCode ArrowDecimalAppendStringToBuffer(const struct ArrowDecimal* decim
ArrowBufferResize(buffer, buffer->size_bytes + num_zeros_after_decimal + 2, 0));

uint8_t* digits_start = buffer->data + is_negative;
memmove(digits_start + num_zeros_after_decimal + 2, digits_start, num_digits);
memmove(digits_start + num_zeros_after_decimal + 2, digits_start,
_ArrowInt64ToSizeT(num_digits));
*digits_start++ = '0';
*digits_start++ = '.';
for (int i = 0; i < num_zeros_after_decimal; i++) {
Expand Down
3 changes: 2 additions & 1 deletion src/nanoarrow/device/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ static ArrowErrorCode ArrowDeviceCpuBufferCopy(struct ArrowDevice* device_src,
return EINVAL;
}

memcpy((uint8_t*)dst.data.as_uint8, src.data.as_uint8, dst.size_bytes);
memcpy((uint8_t*)dst.data.as_uint8, src.data.as_uint8,
_ArrowInt64ToSizeT(dst.size_bytes));
return NANOARROW_OK;
}

Expand Down
11 changes: 6 additions & 5 deletions src/nanoarrow/ipc/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ static ArrowErrorCode ArrowIpcDecoderVisitMetadata(
int64_t n_pairs = ns(KeyValue_vec_len(kv_vec));

for (int64_t i = 0; i < n_pairs; i++) {
ns(KeyValue_table_t) kv = ns(KeyValue_vec_at(kv_vec, i));
ns(KeyValue_table_t) kv = ns(KeyValue_vec_at(kv_vec, _ArrowInt64ToSizeT(i)));
flatbuffers_string_t key = ns(KeyValue_key(kv));
flatbuffers_string_t value = ns(KeyValue_value(kv));

Expand Down Expand Up @@ -1168,7 +1168,7 @@ static int ArrowIpcDecoderSetTypeUnion(struct ArrowSchema* schema,

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

Expand Down Expand Up @@ -1417,7 +1417,7 @@ static int ArrowIpcDecoderSetChildren(struct ArrowSchema* schema, ns(Field_vec_t
int64_t n_fields = ns(Schema_vec_len(fields));

for (int64_t i = 0; i < n_fields; i++) {
ns(Field_table_t) field = ns(Field_vec_at(fields, i));
ns(Field_table_t) field = ns(Field_vec_at(fields, _ArrowInt64ToSizeT(i)));
NANOARROW_RETURN_NOT_OK(
ArrowIpcDecoderSetField(schema->children[i], field, dictionaries, error));
}
Expand Down Expand Up @@ -1449,7 +1449,7 @@ static int ArrowIpcDecoderDecodeSchemaHeader(struct ArrowIpcDecoder* decoder,
decoder->feature_flags = 0;

for (int64_t i = 0; i < n_features; i++) {
ns(Feature_enum_t) feature = ns(Feature_vec_at(features, i));
ns(Feature_enum_t) feature = ns(Feature_vec_at(features, _ArrowInt64ToSizeT(i)));
switch (feature) {
case ns(Feature_COMPRESSED_BODY):
decoder->feature_flags |= NANOARROW_IPC_FEATURE_COMPRESSED_BODY;
Expand Down Expand Up @@ -2086,7 +2086,8 @@ ArrowErrorCode ArrowIpcDecoderSetSchemaWithDictionaries(
ArrowErrorSet(error, "Failed to allocate decoder->fields");
return ENOMEM;
}
memset(private_data->fields, 0, private_data->n_fields * sizeof(struct ArrowIpcField));
memset(private_data->fields, 0,
_ArrowInt64ToSizeT(private_data->n_fields) * sizeof(struct ArrowIpcField));

// Init field information and calculate starting buffer offset for each
int64_t field_i = 0;
Expand Down
52 changes: 30 additions & 22 deletions src/nanoarrow/ipc/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ void ArrowIpcEncoderSetDictionaryReplacement(struct ArrowIpcEncoder* encoder,
}

static ArrowErrorCode ArrowIpcEncoderWriteContinuationAndSize(struct ArrowBuffer* out,
size_t size) {
_NANOARROW_CHECK_UPPER_LIMIT(size, INT32_MAX);
int64_t size) {
_NANOARROW_CHECK_RANGE(size, 0, INT32_MAX);
NANOARROW_RETURN_NOT_OK(ArrowBufferAppendInt32(out, -1));

if (ArrowIpcSystemEndianness() == NANOARROW_IPC_ENDIANNESS_BIG) {
Expand All @@ -250,7 +250,7 @@ ArrowErrorCode ArrowIpcEncoderFinalizeBuffer(struct ArrowIpcEncoder* encoder,
int64_t padded_size = _ArrowRoundUpToMultipleOf8(size);
NANOARROW_RETURN_NOT_OK(
ArrowBufferReserve(out, sizeof(int32_t) + sizeof(int32_t) + padded_size));
NANOARROW_ASSERT_OK(ArrowIpcEncoderWriteContinuationAndSize(out, padded_size));
NANOARROW_RETURN_NOT_OK(ArrowIpcEncoderWriteContinuationAndSize(out, padded_size));
} else {
NANOARROW_RETURN_NOT_OK(ArrowBufferReserve(out, size));
}
Expand Down Expand Up @@ -503,9 +503,12 @@ static ArrowErrorCode ArrowIpcEncodeMetadata(flatcc_builder_t* builder,
NANOARROW_RETURN_NOT_OK_WITH_ERROR(ArrowMetadataReaderRead(&metadata, &key, &value),
error);
FLATCC_RETURN_UNLESS_0_NO_NS(push_start(builder), error);
FLATCC_RETURN_UNLESS_0(KeyValue_key_create(builder, key.data, key.size_bytes), error);
FLATCC_RETURN_UNLESS_0(KeyValue_value_create(builder, value.data, value.size_bytes),
error);
FLATCC_RETURN_UNLESS_0(
KeyValue_key_create(builder, key.data, _ArrowInt64ToSizeT(key.size_bytes)),
error);
FLATCC_RETURN_UNLESS_0(
KeyValue_value_create(builder, value.data, _ArrowInt64ToSizeT(value.size_bytes)),
error);
FLATCC_RETURN_IF_NULL(push_end(builder), error);
}
return NANOARROW_OK;
Expand Down Expand Up @@ -821,8 +824,9 @@ static ArrowErrorCode ArrowIpcEncoderReserveCompressedBuffers(
}

if (private->n_compressed_buffers > 0) {
memcpy(buffers, private->compressed_buffers,
private->n_compressed_buffers * sizeof(struct ArrowBuffer));
memcpy(
buffers, private->compressed_buffers,
_ArrowInt64ToSizeT(private->n_compressed_buffers) * sizeof(struct ArrowBuffer));
ArrowFree(private->compressed_buffers);
}
for (int64_t i = private->n_compressed_buffers; i < n; i++) {
Expand Down Expand Up @@ -1074,14 +1078,16 @@ static ArrowErrorCode ArrowIpcEncoderEncodeRecordBatch(
ArrowIpcEncoderEncodeRecordBatchImpl(private, array_view, error));
NANOARROW_RETURN_NOT_OK(ArrowIpcEncoderEncodeBuffers(encoder, buffer_encoder, error));

FLATCC_RETURN_UNLESS_0(RecordBatch_nodes_create( //
builder, (struct ns(FieldNode)*)private->nodes.data,
private->nodes.size_bytes / sizeof(struct ns(FieldNode))),
error);
FLATCC_RETURN_UNLESS_0(RecordBatch_buffers_create( //
builder, (struct ns(Buffer)*)private->buffers.data,
private->buffers.size_bytes / sizeof(struct ns(Buffer))),
error);
FLATCC_RETURN_UNLESS_0(
RecordBatch_nodes_create( //
builder, (struct ns(FieldNode)*)private->nodes.data,
_ArrowInt64ToSizeT(private->nodes.size_bytes) / sizeof(struct ns(FieldNode))),
error);
FLATCC_RETURN_UNLESS_0(
RecordBatch_buffers_create( //
builder, (struct ns(Buffer)*)private->buffers.data,
_ArrowInt64ToSizeT(private->buffers.size_bytes) / sizeof(struct ns(Buffer))),
error);

FLATCC_RETURN_UNLESS_0(Message_header_RecordBatch_end(builder), error);

Expand Down Expand Up @@ -1145,12 +1151,14 @@ static ArrowErrorCode ArrowIpcEncoderEncodeDictionaryBatch(
NANOARROW_RETURN_NOT_OK(ArrowIpcEncoderEncodeBuffers(encoder, buffer_encoder, error));

FLATCC_RETURN_UNLESS_0(
RecordBatch_nodes_create(builder, (struct ns(FieldNode)*)private->nodes.data,
private->nodes.size_bytes / sizeof(struct ns(FieldNode))),
RecordBatch_nodes_create(
builder, (struct ns(FieldNode)*)private->nodes.data,
_ArrowInt64ToSizeT(private->nodes.size_bytes) / sizeof(struct ns(FieldNode))),
error);
FLATCC_RETURN_UNLESS_0(
RecordBatch_buffers_create(builder, (struct ns(Buffer)*)private->buffers.data,
private->buffers.size_bytes / sizeof(struct ns(Buffer))),
RecordBatch_buffers_create(
builder, (struct ns(Buffer)*)private->buffers.data,
_ArrowInt64ToSizeT(private->buffers.size_bytes) / sizeof(struct ns(Buffer))),
error);
FLATCC_RETURN_UNLESS_0(DictionaryBatch_data_end(builder), error);
FLATCC_RETURN_UNLESS_0(DictionaryBatch_isDelta_add(builder, is_delta ? 1 : 0), error);
Expand Down Expand Up @@ -1224,7 +1232,7 @@ ArrowErrorCode ArrowIpcEncoderEncodeFooter(struct ArrowIpcEncoder* encoder,

FLATCC_RETURN_UNLESS_0(Footer_recordBatches_start(builder), error);
struct ns(Block)* flatcc_RecordBatch_blocks =
ns(Footer_recordBatches_extend(builder, n_blocks));
ns(Footer_recordBatches_extend(builder, _ArrowInt64ToSizeT(n_blocks)));
FLATCC_RETURN_IF_NULL(flatcc_RecordBatch_blocks, error);
for (int64_t i = 0; i < n_blocks; i++) {
struct ns(Block) block = {
Expand All @@ -1243,7 +1251,7 @@ ArrowErrorCode ArrowIpcEncoderEncodeFooter(struct ArrowIpcEncoder* encoder,

FLATCC_RETURN_UNLESS_0(Footer_dictionaries_start(builder), error);
struct ns(Block)* flatcc_dict_blocks =
ns(Footer_dictionaries_extend(builder, n_dict_blocks));
ns(Footer_dictionaries_extend(builder, _ArrowInt64ToSizeT(n_dict_blocks)));
FLATCC_RETURN_IF_NULL(flatcc_dict_blocks, error);
for (int64_t i = 0; i < n_dict_blocks; i++) {
struct ns(Block) block = {
Expand Down
Loading
Loading