Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added is_type policy function #5691

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

victormlg
Copy link
Contributor

Changelog: Added is_type policy function to check type of a variable.
Ticket: CFE-3641

libpromises/evalfunction.c Fixed Show fixed Hide fixed
Signed-off-by: Victor Moene <[email protected]>
Changelog: Added is_type policy function to check type of a variable.
Ticket: CFE-3641
Copy link
Contributor

@larsewi larsewi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some comments that you need to address. Otherwise, great work 🚀 Could you also make an acceptance test? This way we know for certain that it works, and that it continues working in the future. I'll help you get started if you haven't written one before 😉

libpromises/evalfunction.c Show resolved Hide resolved
// parse type
const char* const type_arg_name = TrimWhitespace(RlistScalarValue(type_arg));
const char* const regex = " ";
const Rlist *const first_arg = RlistFromRegexSplitNoOverflow(type_arg_name, regex, 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a regex for splitting a string on a white space character is a bit overkill. See if you can find an easier way to split the string. Also the RlistFromRegexSplitNoOverflow() function may return error. So make sure that it is handled gracefully. We don't want the program to crash if the user uses the function wrong. Instead we would want to print an error message and return failure.

libpromises/evalfunction.c Outdated Show resolved Hide resolved
libpromises/evalfunction.c Outdated Show resolved Hide resolved
{
Log(LOG_LEVEL_ERR, "Function %s : invalid datatype",
fp->name);
return FnFailure();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an error? Should it not return FnReturnContext(false)?

@victormlg victormlg requested a review from olehermanse January 29, 2025 16:42
@@ -5276,38 +5276,21 @@ static FnCallResult FnCallFold(EvalContext *ctx,

/*********************************************************************/

static FnCallResult FnCallDatatype(EvalContext *ctx, ARG_UNUSED const Policy *policy, const FnCall *fp, const Rlist *finalargs)
static const char * DataTypeStringFromVarName(EvalContext *ctx, const char *var_name, bool detail)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const char * commonly means / implies a string owned by something else, or in other words a string you don't need to free.

{
detail = BooleanFromString(RlistScalarValue(finalargs->next));
}
const char * const ouptput_string = DataTypeStringFromVarName(ctx, var_name, detail);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never freed.

}
return StringConcatenate(2, "policy ", type_str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never freed.

}

return FnReturnF("%s %s", type_str, subtype_str);
return StringConcatenate(3, type_str, " ", subtype_str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never freed.

const char *const type_str =
(type == CF_DATA_TYPE_NONE) ? "none" : DataTypeToString(type);

if (!detail)
{
return FnReturn(type_str);
return type_str;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return type_str;
return StringDuplicate(type_str);

@@ -5276,38 +5276,21 @@ static FnCallResult FnCallFold(EvalContext *ctx,

/*********************************************************************/

static FnCallResult FnCallDatatype(EvalContext *ctx, ARG_UNUSED const Policy *policy, const FnCall *fp, const Rlist *finalargs)
static const char * DataTypeStringFromVarName(EvalContext *ctx, const char *var_name, bool detail)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static const char * DataTypeStringFromVarName(EvalContext *ctx, const char *var_name, bool detail)
static char * DataTypeStringFromVarName(EvalContext *ctx, const char *var_name, bool detail)

{
detail = BooleanFromString(RlistScalarValue(finalargs->next));
}
const char * const ouptput_string = DataTypeStringFromVarName(ctx, var_name, detail);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const char * const ouptput_string = DataTypeStringFromVarName(ctx, var_name, detail);
char * const ouptput_string = DataTypeStringFromVarName(ctx, var_name, detail);

return FnFailure();
}

return FnReturnF("%s", ouptput_string);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how you can "give" it to the caller, and it's their job to free it.

Suggested change
return FnReturnF("%s", ouptput_string);
return FnReturnNoCopy(output_string);

detail = true;
}
}
const char * const type_string = DataTypeStringFromVarName(ctx, var_name, detail);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never freed.

detail = true;
}
}
const char * const type_string = DataTypeStringFromVarName(ctx, var_name, detail);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const char * const type_string = DataTypeStringFromVarName(ctx, var_name, detail);
char * const type_string = DataTypeStringFromVarName(ctx, var_name, detail);

Comment on lines +5410 to +5411

return FnReturnContext(StringEqual(type_name, type_string));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return FnReturnContext(StringEqual(type_name, type_string));
const bool matching = StringEqual(type_name, type_string);
free(type_string);
return FnReturnContext(matching);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants