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

Add C++ FFI wrappers and use them in CLI. #2133

Merged
merged 6 commits into from
Nov 27, 2023
Merged

Conversation

ni4
Copy link
Contributor

@ni4 ni4 commented Oct 13, 2023

Later, once filled up, we could export this header as well for C++ library users.

@ni4 ni4 force-pushed the ni4-add-fficpp-header branch from a108e4a to 80d5b25 Compare October 13, 2023 08:37
@codecov
Copy link

codecov bot commented Oct 13, 2023

Codecov Report

Attention: 52 lines in your changes are missing coverage. Please review.

Comparison is base (b02c64e) 76.88% compared to head (c6c702e) 76.91%.

Files Patch % Lines
src/rnp/rnpcpp.hpp 81.08% 28 Missing ⚠️
src/rnp/fficli.cpp 83.67% 24 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2133      +/-   ##
==========================================
+ Coverage   76.88%   76.91%   +0.02%     
==========================================
  Files         193      194       +1     
  Lines       36976    37042      +66     
==========================================
+ Hits        28430    28490      +60     
- Misses       8546     8552       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ni4 ni4 force-pushed the ni4-add-fficpp-header branch 4 times, most recently from 6c884bd to abcb26c Compare October 31, 2023 11:46
@ni4 ni4 force-pushed the ni4-add-fficpp-header branch from abcb26c to c6c702e Compare November 27, 2023 09:23
@ni4 ni4 marked this pull request as ready for review November 27, 2023 09:25
@ni4
Copy link
Contributor Author

ni4 commented Nov 27, 2023

@antonsviridenko @maxirmx @ronaldtse Could you please check whether this approach is good to you? Basically, idea is to add C++ header wrapper around C calls, to make things easier regarding to the memory management (and for those who would like to use library from C++ code). Currently it implements just few functions, but later it could be finalized to the header with full functionality, sitting next to rnp/rnp.h. Thanks!

Copy link
Member

@maxirmx maxirmx left a comment

Choose a reason for hiding this comment

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

IMHO there is something wrong with error handling

If you look at the typical call sequence

--> Key::alg --> rnp_key_get_alg -->

rnp_key_get_alg - returns either RNP_SUCCESS which is NULL or catches an exception and returns ffi_exception object with detailed info (FFI_GUARD macro)

Key::alg - if rnp_key_get_alg return is not RNP_SUCCESS, creates another exception with less error info and throws it (CALL_FFI macro)

I think

#define CALL_FFI(func_call)                           \
    do {                                              \
        auto __ret__ = func_call;                     \
        if (__ret__) {                                \
            throw ffi_exception(__ret__, #func_call); \
        }                                             \
    } while (0)

shall be somthing like

template<typename Func, typename... Args>
void call_ffi(Func&& func, Args&&... args) {
    auto __ret__ = func(std::forward<Args>(args)...);
    if (__ret__) {
        throw __ret__;
    }
}

so that an exceptions returned from rnp_key_get_alg and similar functions are propagated

It is also very possible that I did not get the idea :)

@ni4
Copy link
Contributor Author

ni4 commented Nov 27, 2023

@maxirmx rnp_do_something() functions are designed to be C-only and not to throw an exception and just return some error code, that is guarded via try { ... } FFI_GUARD , where later expands to

static uint32_t
ffi_exception(FILE *fp, const char *func, const char *msg, uint32_t ret = RNP_ERROR_GENERIC)
{
    if (rnp_log_switch()) {
        fprintf(
          fp, "[%s()] Error 0x%08X (%s): %s\n", func, ret, rnp_result_to_string(ret), msg);
    }
    return ret;
}

#define FFI_GUARD_FP(fp)                                                            \
    catch (rnp::rnp_exception & e)                                                  \
    {                                                                               \
        return ffi_exception((fp), __func__, e.what(), e.code());                   \
    }                                                                               \
    catch (std::bad_alloc &)                                                        \
    {                                                                               \
        return ffi_exception((fp), __func__, "bad_alloc", RNP_ERROR_OUT_OF_MEMORY); \
    }                                                                               \
    catch (std::exception & e)                                                      \
    {                                                                               \
        return ffi_exception((fp), __func__, e.what());                             \
    }                                                                               \
    catch (...)                                                                     \
    {                                                                               \
        return ffi_exception((fp), __func__, "unknown exception");                  \
    }

#define FFI_GUARD FFI_GUARD_FP((stderr))

@antonsviridenko antonsviridenko merged commit e9021c4 into main Nov 27, 2023
114 checks passed
@antonsviridenko antonsviridenko deleted the ni4-add-fficpp-header branch November 27, 2023 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants