Not logged in
Log in now
Create an account
Subscribe to LWN
An "enum" for Python 3
An unexpected perf feature
LWN.net Weekly Edition for May 16, 2013
A look at the PyPy 2.0 release
PostgreSQL 9.3 beta: Federated databases and more
That said, one size doesn't fit all so having different function is reasonable, the biggest issue is that there is no sane default behaviour..
The ups and downs of strlcpy()
Posted Jul 26, 2012 16:27 UTC (Thu) by nybble41 (subscriber, #55106)
The strxcpy function isn't just a wrapper; it does all of the real work. The strxcpy_abort, strxcpy_truncate functions only run when an overflow condition is detected. This allows you to substitute your own preferred method of error-handling.
This is actually rather similar to the way exceptions are handled in Common Lisp or Scheme programs, except that the Lisp version would use dynamic variables rather than explicit arguments for the handler code, which results in less cluttered code.
(define (default-error-handler error-value) (abort))
(define current-error-handler (make-parameter default-error-handler))
(... (if (ok? var) var ((current-error-handler) var)) ... ))
; aborts on error
; evaluates to #t on success, or #f on error
(parameterize ([current-error-handler (lambda _ (return #f))])
; uses "value" in place of var on error
(parameterize ([current-error-handler (lambda _ value)])
Scheme-style parameters are attached to the current continuation, meaning that they're not only thread-safe, but that the bindings only affect the inner dynamic scope of the (parameterize) form, even in exceptional cases such as non-local return (like the middle example above) and even re-entry into a dynamic scope which was previously exited.
Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds