<div dir="ltr">Eric,<div>Thanks for the insights. I have posted a patch at <a href="http://review.gluster.org/6201">http://review.gluster.org/6201</a> which clarifies the usage of glfs_readdir_r() and also introduce glfs_readdir().</div>
<div><br></div><div>Thanks,</div><div>Avati</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Oct 30, 2013 at 11:05 AM, Eric Blake <span dir="ltr">&lt;<a href="mailto:eblake@redhat.com" target="_blank">eblake@redhat.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On 10/30/2013 11:18 AM, Eric Blake wrote:<br>
<br>
&gt; The only safe way to use readdir_r is to know the maximum d_name that<br>
&gt; can possibly be returned, but there is no glfs_fpathconf() for<br>
&gt; determining that information.  Your example usage of glfs_readdir_r()<br>
&gt; suggests that 512 bytes is large enough:<br>
&gt; <a href="https://forge.gluster.org/glusterfs-core/glusterfs/blobs/f44ada6cd9bcc5ab98ca66bedde4fe23dd1c3f05/api/examples/glfsxmp.c" target="_blank">https://forge.gluster.org/glusterfs-core/glusterfs/blobs/f44ada6cd9bcc5ab98ca66bedde4fe23dd1c3f05/api/examples/glfsxmp.c</a><br>

&gt; but I don&#39;t know if that is true.<br>
<br>
</div>Okay, after a bit more investigation, I see:<br>
<br>
gf_dirent_to_dirent (gf_dirent_t *gf_dirent, struct dirent *dirent)<br>
{<br>
        dirent-&gt;d_ino = gf_dirent-&gt;d_ino;<br>
<br>
#ifdef _DIRENT_HAVE_D_OFF<br>
        dirent-&gt;d_off = gf_dirent-&gt;d_off;<br>
#endif<br>
<br>
#ifdef _DIRENT_HAVE_D_TYPE<br>
        dirent-&gt;d_type = gf_dirent-&gt;d_type;<br>
#endif<br>
<br>
#ifdef _DIRENT_HAVE_D_NAMLEN<br>
        dirent-&gt;d_namlen = strlen (gf_dirent-&gt;d_name);<br>
#endif<br>
<br>
        strncpy (dirent-&gt;d_name, gf_dirent-&gt;d_name, 256);<br>
}<br>
<br>
I also discovered that &#39;getconf NAME_MAX /path/to/xfs/mount&#39; is 255, so<br>
it looks like you got lucky (although strncpy is generally unsafe<br>
because it fails to write a NUL terminator if you truncate the string,<br>
it looks like you are guaranteed by XFS to never have a string that<br>
needs truncation).<br>
<div class="im"><br>
&gt;  You _do_ have the advantage that<br>
&gt; since every brick backing a glusterfs volume is using an xfs file<br>
&gt; system, then you only have to worry about the NAME_MAX of xfs - but I<br>
&gt; don&#39;t know that value off the top of my head.<br>
<br>
</div>Again, my research shows it is 255.<br>
<div class="im"><br>
&gt;  Can you please let me<br>
&gt; know how big I should make my struct dirent to avoid buffer overflow,<br>
&gt; and properly document this in &lt;glusterfs/api/glfs.h&gt;?  Furthermore, can<br>
&gt; you please provide a much saner glfs_readdir() so I don&#39;t have to worry<br>
&gt; about contortions of using a broken-by-design function?<br>
<br>
</div>These requests are still in force.<br>
<div class="HOEnZb"><div class="h5"><br>
--<br>
Eric Blake   eblake redhat com    <a href="tel:%2B1-919-301-3266" value="+19193013266">+1-919-301-3266</a><br>
Libvirt virtualization library <a href="http://libvirt.org" target="_blank">http://libvirt.org</a><br>
<br>
</div></div><br>_______________________________________________<br>
Gluster-devel mailing list<br>
<a href="mailto:Gluster-devel@nongnu.org">Gluster-devel@nongnu.org</a><br>
<a href="https://lists.nongnu.org/mailman/listinfo/gluster-devel" target="_blank">https://lists.nongnu.org/mailman/listinfo/gluster-devel</a><br>
<br></blockquote></div><br></div>