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

Do not use assert() for checking OS API functions #40

Merged
merged 8 commits into from
Nov 5, 2015

Conversation

troglobit
Copy link
Collaborator

This pull request is a mix of .gitignore updates, doc comment fixes from
previous pull request, whitespace and readability cleanup. But the most
important patch is the last one which replaces the use of assert() as a
means of verifying OS memory allocation with NULL returns.

…mples

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
- Simplify code, no need for else if' when previous 'if' always returns
- Add whitespace to sectionalize code, readability
- Return NULL, not 0, for pointers
- Remove useless assignment before free()

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
…location.

This patch removes assert()'s used for checking memory allocation:
calloc(), malloc(), strdup().  Instead libConfuse now returns NULL to
allow the caller to handle such errors more gracefully.

Some memory allocations did even have an assert(), they simply assumed
memory had been successfully allocated.  These have also been fixed.

For internal functions which previously asserted, their return value
is now verified by the public API's using them.

Some minor cleanup is also included in this commit:
- Readability: avoid assignment inside conditionals
- Whitespace for sectionalizing code

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
@troglobit
Copy link
Collaborator Author

Force pushed update to last commit after insightful comment on realloc() by @peda-r.

After comment from @peda-r:

- Handle strdup() failure in cfg_dupopt_array()
- Handle strdup() failure in cfg_opt_setnstr()

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
…() [SQUASHME]

The cfg_opt_getval() API now returns NULL, we must check for that now so
we do not dereference invalid pointers.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
…dation

This patch introduces a semantic change in the behavior of libConfuse on
invalid input to API's.  Replacing assert() for argument validation with
a simple if() statement (inverted logic compared to asserts) with errno
set to EINVAL and returning cfg_false, NULL or similar on error.

Some API's, mostly cfg_*set*(), are declared to return void, others,
cfg_*getint/float/bool*(), return the actual data value.  The former set
functions will be changed in a later patch to return a status (POSIX
OK(0) or non-zero on error), and the latter will likely return zero on
error, which means the user will have to consult errno to check for
error.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This patch changes the API for libConfuse set functions to return
the status of the operation: CFG_FAIL, with errno set on error,
and CFG_SUCCESS (POSIX OK) on success.

Also, the API docs in confuse.h is updated with new @return lines.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
@troglobit
Copy link
Collaborator Author

A few more changes related to issue #37 have now been added to this pull request.

  • Fix missing internal checks for return value of cfg_opt_getval() so we don't dereference NULL pointers
  • Replace assert()'s for API input validation with if (...) statements that return CFG_FAIL with errno=EINVAL
  • Add missing return status, CFG_FAIL or CFG_SUCCESS, to libConfuse set functions.

@troglobit troglobit added this to the v3.0 milestone Oct 28, 2015
@troglobit
Copy link
Collaborator Author

I'll let this PR linger a bit more, pending comments from other devs.
Deadline November 1st! 👍

@troglobit troglobit merged commit 6188764 into libconfuse:master Nov 5, 2015
@troglobit troglobit deleted the reduce-asserts branch February 20, 2016 12:42
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.

None yet

2 participants