Skip to content

DRAFT: Non-panicking C API#472

Closed
agerasev wants to merge 1 commit into
linebender:masterfrom
agerasev:no_panic
Closed

DRAFT: Non-panicking C API#472
agerasev wants to merge 1 commit into
linebender:masterfrom
agerasev:no_panic

Conversation

@agerasev
Copy link
Copy Markdown

Hi!

I'm using resvg and it's really impressing. Thank you for your hard work!

But I've faced an issue with c-api. The issue is that C API functions can panic internally due to variety of reasons and therefore terminate the whole application. And I can't see any good way to handle these panics in C code.

Have you considered the possibility of making another C API that reports about all errors by returning an error code rather than panicking? (maybe along with current C API)

Here is my vision of such C API. Could you tell me what do you think about that? Has it a chance to be added to resvg?

@RazrFalcon
Copy link
Copy Markdown
Collaborator

RazrFalcon commented Oct 20, 2021

  1. The library must not panic. If it panics - please report a bug. catch_panic is not a solution.
  2. Your solution is way too complicated. All we have to do is to wrap usvg::Tree::from_data and resvg::render in catch_unwind. It would add 10 LOC. Nothing else can panic.

If by panicking you mean asserts in C API, then we simply have to improve the documentation. You must provide valid arguments. There is no point in returning an error in this case.

@agerasev
Copy link
Copy Markdown
Author

agerasev commented Nov 3, 2021

Ok, now I understand that C API can only panic on breach of contract. It was difficult to see at first.

Although I would prefer error code rather than panicking, it seems reasonable here because it makes the code much simpler.

Thank you for taking a look at the PR, I'm closing it.

@agerasev agerasev closed this Nov 3, 2021
@bravikov
Copy link
Copy Markdown

I think it is a very useful pull request.

The library reacts too brutally to various mistakes.

@RazrFalcon
Copy link
Copy Markdown
Collaborator

RazrFalcon commented Nov 12, 2021

The library itself is very forgiving. C API is not. assert on contract violation is a normal practice for C code.

Can you provide some concrete examples when the C API behaved in an unexpected way?

@leha-bot
Copy link
Copy Markdown

Unfortunately there are no easy way to determine contract violations in any user input, e.g. server-side SVG file(in particular, there are no way it to do it in C API, but It's possible that I don't know about the proper way, sorry), so it would be very convenient to provide some flat C validation APIs maybe partially like this PR that I found, thanks in advance for your attention

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.

4 participants