Skip to content

Refactor OSSL_PARAM_allocate_from_text() for better flexibility#11046

Closed
levitte wants to merge 7 commits intoopenssl:masterfrom
levitte:fix-OSSL_PARAM_allocate_from_text-20200209
Closed

Refactor OSSL_PARAM_allocate_from_text() for better flexibility#11046
levitte wants to merge 7 commits intoopenssl:masterfrom
levitte:fix-OSSL_PARAM_allocate_from_text-20200209

Conversation

@levitte
Copy link
Member

@levitte levitte commented Feb 9, 2020

OSSL_PARAM_allocate_from_text() and OSSL_PARAM_construct_from_text()
were "do-it-all" kind of functions, which didn't allow much
flexibility in code that wants to adapt to situations such as the key
not being found in the descriptor OSSL_PARAM array.

We therefore split the functionality to make key lookup and value
processing two different functions, thus resulting in the following
functions:

  • OSSL_PARAM_parse_locate_const(), which parses the key and sets flags
    accordingly, but otherwise works like OSSL_PARAM_locate_const().
  • OSSL_PARAM_construct_from_text(), which doesn't look up the key any
    more, but rather only parses the given value, with direction from a
    template OSSL_PARAM item, which could come from a call of
    OSSL_PARAM_parse_locate_const().
  • OSSL_PARAM_allocate_from_text(), which is modified exactly like
    OSSL_PARAM_construct_from_text().

With this split, callers can also ignore OSSL_PARAM_parse_locate_const()
completely, and work with an descriptor OSSL_PARAM item of their own
choosing.

@levitte levitte requested a review from romen February 9, 2020 12:42
@levitte levitte added approval: review pending This pull request needs review by a committer branch: master Applies to master branch labels Feb 9, 2020
@romen
Copy link
Member

romen commented Feb 9, 2020

Travis red cross is relevant (duplicated NOTES section)

@levitte
Copy link
Member Author

levitte commented Feb 9, 2020

Travis red cross is relevant (duplicated NOTES section)

Thanks. Fixed

Copy link
Contributor

@paulidale paulidale left a comment

Choose a reason for hiding this comment

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

All this change for one instance where the return is used?
I'm dubious as to the value this provides.

Is it possible to instead consider a sequence like:


if ((p = OSSL_PARAM_locate_const(setable, "name")) == NULL)
    return -2;
if (!OSSL_PARAM_allocate_from_text(&params[0], p, name, value, strlen(value)))
    return 0;

It's less of a kludge, the same number of lines of code, clearer and no slower.

@levitte
Copy link
Member Author

levitte commented Feb 10, 2020

@paulidale, I was going to suggest exactly what you suggest, until I realised that it's not that simple, the caller must then also deal with any key variant (such as names prefixed with "hex").

As for only one spot, I'm pretty sure we have more than one ctrl_str function, no? I haven't looked at the others yet.

@paulidale
Copy link
Contributor

Good point about the hex version. I still think returning the information via a misused structure field isn't ideal.

Is a ossl_param_locate_ctrl_str function a possibility?
It would be less mystifying to use than this PR.

@paulidale
Copy link
Contributor

paulidale commented Feb 10, 2020

The function would be:

OSSL_PARAM *ossl_param_locate_ctrl_str(const OSSL_PARAM *paramdefs,
                                       const char *key, int *ishex)
{
    *ishex = strncmp(key, "hex", 3) == 0;
    if (*ishex)
        key += 3;
    return OSSL_PARAM_locate_const(paramdefs, key);
}

@levitte
Copy link
Member Author

levitte commented Feb 10, 2020

This function is also used in apps/mac.c, and I really abhor having to use internal functions in the app. Eating our own dog food and all that.

@paulidale
Copy link
Contributor

paulidale commented Feb 10, 2020

Either make the new function public or don't distinguish "unknown parameter" from other possible failures.

I abhor magic flags which is what this boils down to.

Changing the OSSL_PARAM_allocate_from_text() so it returns -2 for "unknown parameter" isn't ideal but is at least consistent with other APIs.

@levitte
Copy link
Member Author

levitte commented Feb 10, 2020

Re names, would you do with OSSL_PARAM_parse_locate_const()? It's not reserved for ctrl_str specifically.

@levitte levitte changed the title OSSL_PARAM_allocate_from_text(): Allow detection of unsupported param keys [WIP] OSSL_PARAM_allocate_from_text(): Allow detection of unsupported param keys Feb 10, 2020
@paulidale
Copy link
Contributor

What about #11049 ?

@levitte
Copy link
Member Author

levitte commented Feb 10, 2020

Damn it, I was warming up on the idea of a special locate function...

@paulidale
Copy link
Contributor

It's only a suggestion.

@levitte levitte changed the title [WIP] OSSL_PARAM_allocate_from_text(): Allow detection of unsupported param keys [WIP] Refactor OSSL_PARAM_allocate_from_text() for better flexibility Feb 10, 2020
@levitte
Copy link
Member Author

levitte commented Feb 10, 2020

I have now reworked this to have a separate locator function.

@levitte
Copy link
Member Author

levitte commented Feb 10, 2020

I think I've fixed the Travis failure.

@levitte levitte changed the title [WIP] Refactor OSSL_PARAM_allocate_from_text() for better flexibility Refactor OSSL_PARAM_allocate_from_text() for better flexibility Feb 10, 2020
@levitte
Copy link
Member Author

levitte commented Feb 10, 2020

Not WIP any more, I'm quite satisfied with the result by now. Please review (or if you prefer #11049, please say so)

Copy link
Contributor

@paulidale paulidale left a comment

Choose a reason for hiding this comment

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

I wonder if this is over complicating a public API. For applications that don't care about the reason for failure OSSL_PARAM_allocate_from_text has become more complicated to use -- it's now two calls and an extra local variable is required.

I quite like that OSSL_PARAM_allocate_from_text did the search.

I prefer #11049 from a user's perspective.

@paulidale
Copy link
Contributor

BTW: most internal uses of OSSL_PARAM_allocate_from_text don't seem to care about the reason for failure.

@levitte levitte force-pushed the fix-OSSL_PARAM_allocate_from_text-20200209 branch 2 times, most recently from 9580cdb to 4d1e70c Compare February 13, 2020 08:32
@levitte
Copy link
Member Author

levitte commented Feb 13, 2020

Fixed Travis failures (C++ builds with our headers! I haven't seen those fail in a while... and that, just because I dared call a parameter template)

@levitte
Copy link
Member Author

levitte commented Feb 13, 2020

As to the choice between this and #11049, don't ask me to make it. I'm obviously biased

@paulidale
Copy link
Contributor

The more I think about it, the more I prefer the #11049 approach. I've a conflict of interest too.

@levitte
Copy link
Member Author

levitte commented Feb 13, 2020

So, er, would someone else please step in and make a judgment call? @openssl

@t8m
Copy link
Member

t8m commented Feb 14, 2020

Just my opinion: Although this PR is slightly cleaner architecturally from the callers POV I really prefer #11049 as being more simple to use.

@slontis
Copy link
Member

slontis commented Feb 16, 2020

I really prefer #11049 as being more simple to use.

I agree with Tomas. API simplicity should win.

@paulidale
Copy link
Contributor

An option for future expansion would be to return an unsigned value which contains flags rather than a found/not found Boolean. I can't think of a use for this but it would be more future proof.

@levitte
Copy link
Member Author

levitte commented Feb 17, 2020

@paulidale, how does what you just wrote apply to this PR?

@paulidale
Copy link
Contributor

It's for/about #11049 but the discussion is happening here...

@levitte
Copy link
Member Author

levitte commented Feb 17, 2020

Yeah, that's not confusing at all...

@levitte
Copy link
Member Author

levitte commented Feb 19, 2020

@t8m, @slontis, so go approve it? Or this one if you change your mind.

OSSL_PARAM_allocate_from_text() and OSSL_PARAM_construct_from_text()
were "do-it-all" kind of functions, which didn't allow much
flexibility in code that wants to adapt to situations such as the key
not being found in the descriptor OSSL_PARAM array.

We therefore split the functionality to make key lookup and value
processing two different functions, thus resulting in the following
functions:

- OSSL_PARAM_parse_locate_const(), which parses the key and sets flags
  accordingly, but otherwise works like OSSL_PARAM_locate_const().
- OSSL_PARAM_construct_from_text(), which doesn't look up the key any
  more, but rather only parses the given value, with direction from a
  template OSSL_PARAM item, which could come from a call of
  OSSL_PARAM_parse_locate_const().
- OSSL_PARAM_allocate_from_text(), which is modified exactly like
  OSSL_PARAM_construct_from_text().

With this split, callers can also ignore OSSL_PARAM_parse_locate_const()
completely, and work with an descriptor OSSL_PARAM item of their own
choosing.
Now that locating the key in the settables is done with
OSSL_PARAM_parse_locate_const(), legacy_ctrl_str_to_param() has enough
information to return -2 when the given key isn't supported.
C++ (enable-buildtest-c++) didn't quite like a parameter named 'template'
wonder why...  ;-)
@levitte levitte force-pushed the fix-OSSL_PARAM_allocate_from_text-20200209 branch from 90a6592 to 4c1bb78 Compare February 19, 2020 21:03
@levitte
Copy link
Member Author

levitte commented Feb 20, 2020

Choice made: #11049 (review)
Closing this.

@levitte levitte closed this Feb 20, 2020
@levitte levitte deleted the fix-OSSL_PARAM_allocate_from_text-20200209 branch February 20, 2020 12:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: review pending This pull request needs review by a committer branch: master Applies to master branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants