added error immunity check for the log_write callback

This commit is contained in:
hyunghwan.chung 2018-02-22 04:44:34 +00:00
parent fb3919786f
commit 420ee09907
5 changed files with 49 additions and 17 deletions

View File

@ -331,8 +331,17 @@ const moo_ooch_t* moo_backuperrmsg (moo_t* moo)
return moo->errmsg.tmpbuf.ooch; 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) void moo_seterrwithsyserr (moo_t* moo, int syserr)
{ {
if (moo->shuterr) return;
if (moo->vmprim.syserrstrb) if (moo->vmprim.syserrstrb)
{ {
moo->vmprim.syserrstrb (moo, syserr, moo->errmsg.tmpbuf.bch, MOO_COUNTOF(moo->errmsg.tmpbuf.bch)); 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) #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, ...) 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 - "; static moo_bch_t syntax_error[] = "syntax error - ";
if (moo->shuterr) return;
if (msgfmt) if (msgfmt)
{ {
va_list ap; 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 - "; static moo_bch_t syntax_error[] = "syntax error - ";
if (moo->shuterr) return;
if (msgfmt) if (msgfmt)
{ {
va_list ap; va_list ap;

View File

@ -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 */ /* no line ending - append a line terminator */
moo->log.ptr[moo->log.len++] = '\n'; 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; moo->log.len = 0;
} }
@ -246,7 +246,7 @@ redo:
/* no line ending - append a line terminator */ /* no line ending - append a line terminator */
moo->log.ptr[moo->log.len++] = '\n'; 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; 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->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; moo->log.len = 0;
} }
@ -341,7 +341,7 @@ redo:
/* no line ending - append a line terminator */ /* no line ending - append a line terminator */
moo->log.ptr[moo->log.len++] = '\n'; 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; 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') 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; moo->log.len = 0;
} }
return (x <= -1)? -1: fo.count; 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') 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; 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; va_list ap;
moo_fmtout_t fo; moo_fmtout_t fo;
if (moo->shuterr) return;
moo->errnum = errnum; moo->errnum = errnum;
moo->errmsg.len = 0; 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; va_list ap;
moo_fmtout_t fo; moo_fmtout_t fo;
if (moo->shuterr) return;
moo->errnum = errnum; moo->errnum = errnum;
moo->errmsg.len = 0; 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; moo_fmtout_t fo;
if (moo->shuterr) return;
moo->errnum = errnum; moo->errnum = errnum;
moo->errmsg.len = 0; 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; moo_fmtout_t fo;
if (moo->shuterr) return;
moo->errnum = errnum; moo->errnum = errnum;
moo->errmsg.len = 0; moo->errmsg.len = 0;

View File

@ -978,6 +978,16 @@ enum moo_bcode_t
BCODE_NOOP = 0xFF 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) #if defined(__cplusplus)
extern "C" { extern "C" {
#endif #endif

View File

@ -175,7 +175,7 @@ void moo_fini (moo_t* moo)
{ {
/* flush pending log messages just in case. */ /* flush pending log messages just in case. */
MOO_ASSERT (moo, moo->log.ptr != MOO_NULL); 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) 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 * this point because one of the callbacks could arrange to stop
* logging */ * logging */
MOO_ASSERT (moo, moo->log.ptr != MOO_NULL); 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 */ /* deregister all callbacks */

View File

@ -1161,7 +1161,6 @@ struct moo_t
moo_mmgr_t* mmgr; moo_mmgr_t* mmgr;
moo_cmgr_t* cmgr; moo_cmgr_t* cmgr;
moo_errnum_t errnum; moo_errnum_t errnum;
struct struct
{ {
union union
@ -1173,6 +1172,7 @@ struct moo_t
moo_ooch_t buf[2048]; moo_ooch_t buf[2048];
moo_oow_t len; moo_oow_t len;
} errmsg; } errmsg;
int shuterr;
struct 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 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 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 #else
# define moo_getmmgr(moo) ((moo)->mmgr) # define moo_getmmgr(moo) ((moo)->mmgr)
# define moo_getxtn(moo) ((void*)((moo) + 1)) # 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_setcmgr(moo,mgr) ((moo)->cmgr = (mgr))
# define moo_geterrnum(moo) ((moo)->errnum) # define moo_geterrnum(moo) ((moo)->errnum)
# define moo_seterrnum(moo,num) ((moo)->errmsg.len = 0, (moo)->errnum = (num))
#endif #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_EXPORT void moo_seterrbfmt (
moo_t* moo, 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_EXPORT const moo_ooch_t* moo_geterrstr (
moo_t* moo moo_t* moo
); );