Skip to content

Commit 14c69cd

Browse files
committed
Extended validation.
Signed-off-by: Michal Zientkiewicz <michalz@nvidia.com>
1 parent be2320d commit 14c69cd

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

dali/c_api_2/pipeline.cc

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,15 @@ void AddArgToSpec(dali::OpSpec &spec, const daliArgDesc_t &arg) {
540540
});
541541
};
542542

543+
544+
auto check_arr = [&]() {
545+
if (arg.size < 0)
546+
throw std::invalid_argument(
547+
dali::make_string("`arg.size` of argument \"", name, "\" has a negative size"));
548+
if (arg.size > 0)
549+
check_not_null(arg.arr, "arg.arr");
550+
};
551+
543552
switch (arg.dtype) {
544553
// --- scalar types ---
545554
case DALI_INT8:
@@ -582,29 +591,25 @@ void AddArgToSpec(dali::OpSpec &spec, const daliArgDesc_t &arg) {
582591
break;
583592
// --- vector (list) types ---
584593
case DALI_INT_VEC: {
585-
if (arg.size > 0)
586-
check_not_null(arg.arr, "arg.arr");
594+
check_arr();
587595
auto *d = static_cast<const int *>(arg.arr);
588596
spec.AddArg(name, std::vector<int>(d, d + arg.size));
589597
break;
590598
}
591599
case DALI_FLOAT_VEC: {
592-
if (arg.size > 0)
593-
check_not_null(arg.arr, "arg.arr");
600+
check_arr();
594601
auto *d = static_cast<const float *>(arg.arr);
595602
spec.AddArg(name, std::vector<float>(d, d + arg.size));
596603
break;
597604
}
598605
case DALI_BOOL_VEC: {
599-
if (arg.size > 0)
600-
check_not_null(arg.arr, "arg.arr");
606+
check_arr();
601607
auto *d = static_cast<const bool *>(arg.arr);
602608
spec.AddArg(name, std::vector<bool>(d, d + arg.size));
603609
break;
604610
}
605611
case DALI_STRING_VEC: {
606-
if (arg.size > 0)
607-
check_not_null(arg.arr, "arg.arr");
612+
check_arr();
608613
auto *d = static_cast<const char * const *>(arg.arr);
609614
std::vector<std::string> sv;
610615
sv.reserve(arg.size);
@@ -640,12 +645,12 @@ daliResult_t daliPipelineAddExternalInput(
640645
DALI_EPILOG();
641646
}
642647

643-
daliResult_t daliPipelineAddOperator(
644-
daliPipeline_h pipeline,
645-
const daliOperatorDesc_t *op_desc) {
646-
DALI_PROLOG();
647-
auto pipe = ToPointer(pipeline);
648+
inline dali::OpSpec MakeOpSpec(const daliOperatorDesc_t *op_desc) {
648649
NOT_NULL(op_desc);
650+
dali::c_api::CheckArg(op_desc->num_inputs >= 0, "`num_inputs` must not be negative.");
651+
dali::c_api::CheckArg(op_desc->num_outputs >= 0, "`num_outputs` must not be negative.");
652+
dali::c_api::CheckArg(op_desc->num_args >= 0, "`num_args` must not be negative.");
653+
dali::c_api::CheckArg(op_desc->num_arg_inputs>= 0, "`num_arg_inputs` must not be negative.");
649654
NOT_NULL(op_desc->schema_name);
650655
if (op_desc->num_inputs > 0)
651656
NOT_NULL(op_desc->inputs);
@@ -690,6 +695,15 @@ daliResult_t daliPipelineAddOperator(
690695
});
691696
AddArgToSpec(spec, op_desc->args[i]);
692697
}
698+
return spec;
699+
}
700+
701+
daliResult_t daliPipelineAddOperator(
702+
daliPipeline_h pipeline,
703+
const daliOperatorDesc_t *op_desc) {
704+
DALI_PROLOG();
705+
auto pipe = ToPointer(pipeline);
706+
auto spec = MakeOpSpec(op_desc);
693707
if (op_desc->instance_name && op_desc->instance_name[0] != '\0')
694708
pipe->Unwrap()->AddOperator(spec, op_desc->instance_name);
695709
else
@@ -712,12 +726,19 @@ daliResult_t daliPipelineSetOutputs(
712726
descs.reserve(num_outputs);
713727
for (int i = 0; i < num_outputs; i++) {
714728
NOT_NULL(outputs[i].name);
729+
715730
dali::PipelineOutputDesc desc;
716731
desc.name = outputs[i].name;
717732
desc.device = static_cast<dali::StorageDevice>(outputs[i].device);
733+
718734
if (outputs[i].dtype_present) desc.dtype = outputs[i].dtype;
719-
if (outputs[i].ndim_present) desc.ndim = outputs[i].ndim;
720735
if (outputs[i].layout) desc.layout = outputs[i].layout;
736+
737+
if (outputs[i].ndim_present) {
738+
ValidateNDim(outputs[i].ndim);
739+
desc.ndim = outputs[i].ndim;
740+
}
741+
721742
descs.push_back(std::move(desc));
722743
}
723744
pipe->Unwrap()->SetOutputDescs(std::move(descs));

0 commit comments

Comments
 (0)