From 420ee0990792b6944ddca881c3eca0c94aacadfc Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Thu, 22 Feb 2018 04:44:34 +0000 Subject: [PATCH] added error immunity check for the log_write callback --- moo/lib/err.c | 14 +++++++++++++- moo/lib/logfmt.c | 20 ++++++++++++++------ moo/lib/moo-prv.h | 10 ++++++++++ moo/lib/moo.c | 4 ++-- moo/lib/moo.h | 18 ++++++++++-------- 5 files changed, 49 insertions(+), 17 deletions(-) diff --git a/moo/lib/err.c b/moo/lib/err.c index fc38cc3..95363f5 100644 --- a/moo/lib/err.c +++ b/moo/lib/err.c @@ -331,8 +331,17 @@ const moo_ooch_t* moo_backuperrmsg (moo_t* moo) return moo->errmsg.tmpbuf.ooch; } +void moo_seterrnum (moo_t* moo, moo_errnum_t errnum) +{ + if (moo->shuterr) return; + moo->errnum = errnum; + moo->errmsg.len = 0; +} + void moo_seterrwithsyserr (moo_t* moo, int syserr) { + if (moo->shuterr) return; + if (moo->vmprim.syserrstrb) { moo->vmprim.syserrstrb (moo, syserr, moo->errmsg.tmpbuf.bch, MOO_COUNTOF(moo->errmsg.tmpbuf.bch)); @@ -346,13 +355,14 @@ void moo_seterrwithsyserr (moo_t* moo, int syserr) } } - #if defined(MOO_INCLUDE_COMPILER) void moo_setsynerrbfmt (moo_t* moo, moo_synerrnum_t num, const moo_ioloc_t* loc, const moo_oocs_t* tgt, const moo_bch_t* msgfmt, ...) { static moo_bch_t syntax_error[] = "syntax error - "; + if (moo->shuterr) return; + if (msgfmt) { va_list ap; @@ -402,6 +412,8 @@ void moo_setsynerrufmt (moo_t* moo, moo_synerrnum_t num, const moo_ioloc_t* loc, { static moo_bch_t syntax_error[] = "syntax error - "; + if (moo->shuterr) return; + if (msgfmt) { va_list ap; diff --git a/moo/lib/logfmt.c b/moo/lib/logfmt.c index 51aa087..f74fccf 100644 --- a/moo/lib/logfmt.c +++ b/moo/lib/logfmt.c @@ -202,7 +202,7 @@ static int put_ooch (moo_t* moo, moo_oow_t mask, moo_ooch_t ch, moo_oow_t len) /* no line ending - append a line terminator */ moo->log.ptr[moo->log.len++] = '\n'; } - moo->vmprim.log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); + vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); moo->log.len = 0; } @@ -246,7 +246,7 @@ redo: /* no line ending - append a line terminator */ moo->log.ptr[moo->log.len++] = '\n'; } - moo->vmprim.log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); + vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); moo->log.len = 0; } @@ -297,7 +297,7 @@ static int put_oocs (moo_t* moo, moo_oow_t mask, const moo_ooch_t* ptr, moo_oow_ moo->log.ptr[moo->log.len++] = '\n'; } - moo->vmprim.log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); + vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); moo->log.len = 0; } @@ -341,7 +341,7 @@ redo: /* no line ending - append a line terminator */ moo->log.ptr[moo->log.len++] = '\n'; } - moo->vmprim.log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); + vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); moo->log.len = 0; } @@ -639,7 +639,7 @@ moo_ooi_t moo_logbfmt (moo_t* moo, moo_oow_t mask, const moo_bch_t* fmt, ...) if (moo->log.len > 0 && moo->log.ptr[moo->log.len - 1] == '\n') { - moo->vmprim.log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); + vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); moo->log.len = 0; } return (x <= -1)? -1: fo.count; @@ -667,7 +667,7 @@ moo_ooi_t moo_logufmt (moo_t* moo, moo_oow_t mask, const moo_uch_t* fmt, ...) if (moo->log.len > 0 && moo->log.ptr[moo->log.len - 1] == '\n') { - moo->vmprim.log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); + vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); moo->log.len = 0; } @@ -748,6 +748,8 @@ void moo_seterrbfmt (moo_t* moo, moo_errnum_t errnum, const moo_bch_t* fmt, ...) va_list ap; moo_fmtout_t fo; + if (moo->shuterr) return; + moo->errnum = errnum; moo->errmsg.len = 0; @@ -765,6 +767,8 @@ void moo_seterrufmt (moo_t* moo, moo_errnum_t errnum, const moo_uch_t* fmt, ...) va_list ap; moo_fmtout_t fo; + if (moo->shuterr) return; + moo->errnum = errnum; moo->errmsg.len = 0; @@ -782,6 +786,8 @@ void moo_seterrbfmtv (moo_t* moo, moo_errnum_t errnum, const moo_bch_t* fmt, va_ { moo_fmtout_t fo; + if (moo->shuterr) return; + moo->errnum = errnum; moo->errmsg.len = 0; @@ -796,6 +802,8 @@ void moo_seterrufmtv (moo_t* moo, moo_errnum_t errnum, const moo_uch_t* fmt, va_ { moo_fmtout_t fo; + if (moo->shuterr) return; + moo->errnum = errnum; moo->errmsg.len = 0; diff --git a/moo/lib/moo-prv.h b/moo/lib/moo-prv.h index 41d615d..742237d 100644 --- a/moo/lib/moo-prv.h +++ b/moo/lib/moo-prv.h @@ -978,6 +978,16 @@ enum moo_bcode_t BCODE_NOOP = 0xFF }; + +/* i don't want an error raised inside the callback to override + * the existing error number and message. */ +#define vmprim_log_write(moo,mask,ptr,len) do { \ + int shuterr = (moo)->shuterr; \ + (moo)->shuterr = 1; \ + (moo)->vmprim.log_write (moo, mask, ptr, len); \ + (moo)->shuterr = shuterr; \ + } while(0) + #if defined(__cplusplus) extern "C" { #endif diff --git a/moo/lib/moo.c b/moo/lib/moo.c index 440647b..5f4c84f 100644 --- a/moo/lib/moo.c +++ b/moo/lib/moo.c @@ -175,7 +175,7 @@ void moo_fini (moo_t* moo) { /* flush pending log messages just in case. */ MOO_ASSERT (moo, moo->log.ptr != MOO_NULL); - moo->vmprim.log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); + vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); } for (cb = moo->cblist; cb; cb = cb->next) @@ -191,7 +191,7 @@ void moo_fini (moo_t* moo) * this point because one of the callbacks could arrange to stop * logging */ MOO_ASSERT (moo, moo->log.ptr != MOO_NULL); - moo->vmprim.log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); + vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len); } /* deregister all callbacks */ diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 9997d85..9119f75 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -1161,7 +1161,6 @@ struct moo_t moo_mmgr_t* mmgr; moo_cmgr_t* cmgr; moo_errnum_t errnum; - struct { union @@ -1173,6 +1172,7 @@ struct moo_t moo_ooch_t buf[2048]; moo_oow_t len; } errmsg; + int shuterr; struct { @@ -1652,7 +1652,6 @@ MOO_EXPORT void moo_fini ( static MOO_INLINE void moo_setcmgr (moo_t* moo, moo_cmgr_t* cmgr) { moo->cmgr = cmgr; } static MOO_INLINE moo_errnum_t moo_geterrnum (moo_t* moo) { return moo->errnum; } - static MOO_INLINE void moo_seterrnum (moo_t* moo, moo_errnum_t errnum) { moo->errnum = errnum; moo->errmsg.len = 0; } #else # define moo_getmmgr(moo) ((moo)->mmgr) # define moo_getxtn(moo) ((void*)((moo) + 1)) @@ -1661,9 +1660,17 @@ MOO_EXPORT void moo_fini ( # define moo_setcmgr(moo,mgr) ((moo)->cmgr = (mgr)) # define moo_geterrnum(moo) ((moo)->errnum) -# define moo_seterrnum(moo,num) ((moo)->errmsg.len = 0, (moo)->errnum = (num)) #endif +MOO_EXPORT void moo_seterrnum ( + moo_t* moo, + moo_errnum_t errnum +); + +MOO_EXPORT void moo_seterrwithsyserr ( + moo_t* moo, + int syserr +); MOO_EXPORT void moo_seterrbfmt ( moo_t* moo, @@ -1679,11 +1686,6 @@ MOO_EXPORT void moo_seterrufmt ( ... ); -MOO_EXPORT void moo_seterrwithsyserr ( - moo_t* moo, - int syserr -); - MOO_EXPORT const moo_ooch_t* moo_geterrstr ( moo_t* moo );