Improve and harden handling of edge cases in C-API
Merge pull request #781 from mgreter/fix/hardened-api

Improve and harden handling of edge cases in C-API