From: zoulasc Date: Fri, 5 Sep 2014 09:56:16 +0000 (-0400) Subject: factor out the size check for both readdir methods (raven at themaw dot net) X-Git-Url: https://git.fsl.cs.sunysb.edu/?a=commitdiff_plain;h=bb13dea6d0bf378f38a2a009a9802577f5399673;p=am-utils-6.2.git factor out the size check for both readdir methods (raven at themaw dot net) --- diff --git a/ChangeLog b/ChangeLog index b1bc0972..06bdce58 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2014-09-05 Christos Zoulas + + * factor out the size check for both readdir methods + (raven at themaw.net) + 2014-09-03 Christos Zoulas * configure test for 64 bit xdr function (raven at themaw.net) diff --git a/amd/readdir.c b/amd/readdir.c index 511bdc04..4b5e61b3 100644 --- a/amd/readdir.c +++ b/amd/readdir.c @@ -619,6 +619,20 @@ make_entry_chain3(am_node *mp, const am_entry3 *current_chain, int fully_browsab return retval; } +static size_t needroom3(void) +{ + /* + * Check for enough room. This is extremely approximate but should + * be enough space. Really need 2 times: + * (8byte fileid + * 8byte cookie + * 8byte name pointer + * 8byte next entry addres) = sizeof(am_entry3) + * 2byte name + 1byte terminator + * plus the size of the am_dirlist3 structure */ + return ((2 * ((sizeof(am_entry3) + sizeof("..") + 1))) + sizeof(am_dirlist3)); +} + /* This one is called only if map is browsable */ static int amfs_readdir3_browsable(am_node *mp, am_cookie3 cookie, @@ -636,6 +650,7 @@ amfs_readdir3_browsable(am_node *mp, am_cookie3 cookie, plog(XLOG_DEBUG, "amfs_readdir3_browsable gen=%lu, count=%d", gen, count); if (gen == 0) { + size_t needed = needroom3(); /* * In the default instance (which is used to start a search) we return * "." and "..". @@ -645,17 +660,11 @@ amfs_readdir3_browsable(am_node *mp, am_cookie3 cookie, * fairly unbelievable) then tough. */ dlog("%s: default search", __func__); - /* - * Check for enough room. This is extremely approximate but should - * be enough space. Really need 2 times: - * (8byte fileid - * 8byte cookie - * 8byte name pointer - * 8byte next entry addres) = sizeof(*ep) - * 2byte name + 1byte terminator - * plus the dirlist structure */ - if (count < ((2 * ((sizeof(*ep) + sizeof("..") + 1))) + sizeof(*dp))); + + if (count < needed) { + dlog("%s: not enough room %u < %zu", __func__, count, needed); return EINVAL; + } /* * compute # of entries to send in this chain. @@ -797,6 +806,7 @@ amfs_readdir3(am_node *mp, am_cookie3 cookie, /* when gen is 0, we start reading from the beginning of the directory */ if (gen == 0) { + size_t needed = needroom3(); /* * In the default instance (which is used to start a search) we return * "." and "..". @@ -806,18 +816,9 @@ amfs_readdir3(am_node *mp, am_cookie3 cookie, * fairly unbelievable) then tough. */ dlog("%s: default search", __func__); - /* - * Check for enough room. This is extremely approximate but should - * be enough space. Really need 2 times: - * (8byte fileid - * 8byte cookie - * 8byte name pointer - * 8byte next entry addres) = sizeof(*ep) - * 2byte name + 1byte terminator - * plus the dirlist structure */ -#define NEEDROOM3 ((2 * ((sizeof(*ep) + sizeof("..") + 1))) + sizeof(*dp)) - if (count < NEEDROOM3) { - dlog("%s: not enough room %u < %zu", __func__, count, NEEDROOM3); + + if (count < needed) { + dlog("%s: not enough room %u < %zu", __func__, count, needed); return EINVAL; }