[qforeign] examples/Glob.hsc patch for segv on nomatch

Jens Petersen juhp@01.246.ne.jp
09 Jan 2002 23:45:34 +0900


This patch fixes a problem with the glob binding example in
qforeign, where it would segfault when no files were found
for the glob exp.

Btw, Qrczak, have you considered adding the glibc glob flag
extensions to Glob (eg GLOB_BRACE, GLOB_TILDE, etc)?  They
would be nice to have too, but I guess there is the issue of
portability?  In that case perhaps they should then going
into a separate submodule?

Jens

ps There is also still the issue with attr_set in the
Curses.hsc binding, which I haven't managed to resolve yet
(except by working "around" it by using attrset instead).


2002-01-09  Jens Petersen  <juhp@01.246.ne.jp>

	* examples/Glob.hsc (glob): Test pathc before trying
	to read pathv to avoid segfault on nomatch.

Index: examples/Glob.hsc
===================================================================
RCS file: /cvsroot/qforeign/qforeign/examples/Glob.hsc,v
retrieving revision 1.4
diff -u -r1.4 Glob.hsc
--- examples/Glob.hsc	2001/03/18 19:40:00	1.4
+++ examples/Glob.hsc	2002/01/09 14:18:14
@@ -81,8 +81,12 @@
                 (#const GLOB_NOMATCH) -> GlobNoMatch
                 (#const GLOB_NOSPACE) -> GlobNoSpace
                 _                     -> GlobOther
-        pathv <- (#peek glob_t, gl_pathv) ptr
-        paths <- peekArray0 nullPtr pathv >>= mapM peekCString
+        pathc <- (#peek glob_t, gl_pathc) ptr :: IO CUInt
+        paths <- if pathc > 0 then
+		   do
+		   pathv <- (#peek glob_t, gl_pathv) ptr
+		   peekArray0 nullPtr pathv >>= mapM peekCString
+		  else return []
         globfree ptr
         return (res, paths)