zsh-workers
 help / color / mirror / code / Atom feed
* trying dputs
@ 2014-11-20  1:48 Ray Andrews
  2014-11-20 17:05 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Ray Andrews @ 2014-11-20  1:48 UTC (permalink / raw)
  To: Zsh hackers list


int
bin_whence(char *nam, char **argv, Options ops, int func)
{
     HashNode hn;
     Patprog pprog;
     int returnval = 0;
     int printflags = 0;
     int aliasflags;
     int csh, all, v, wd;
     int informed;
     char *cnam, **allmatched = 0;


If I steal 'opts->args' with a test string like this:

//char *garbage = "utter garbage";
//ops->args = &garbage;

... I get what I expect from this:

dputs("\nargs: %s", *(ops->args));

... but if I leave the pointer unmolested, I get a zsh
segmentation fault. Why is that? I'd expect to either
get a real string, or NULL. It's as if 'ops->args' is
dangling.

The function works fine even with the dummy string, so
I'm pretty sure 'ops->args' isn't needed. Still, I'd
say it shouldn't barf if I try to print it.


     /* Check some option information */
     csh = OPT_ISSET(ops,'c');
     v   = OPT_ISSET(ops,'v');
     all = OPT_ISSET(ops,'a');
     wd  = OPT_ISSET(ops,'w');

     if (OPT_ISSET(ops,'w'))


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: trying dputs
  2014-11-20  1:48 trying dputs Ray Andrews
@ 2014-11-20 17:05 ` Bart Schaefer
  2014-11-20 17:27   ` Ray Andrews
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2014-11-20 17:05 UTC (permalink / raw)
  To: Zsh hackers list

On Nov 19,  5:48pm, Ray Andrews wrote:
} 
} If I steal 'opts->args' with a test string like this:
} 
} //char *garbage = "utter garbage";
} //ops->args = &garbage;
} 
} ... I get what I expect from this:
} 
} dputs("\nargs: %s", *(ops->args));
} 
} ... but if I leave the pointer unmolested, I get a zsh
} segmentation fault. Why is that?

Because ops->args is a NULL pointer (not a pointer to an empty string)
when there are no arguments to the option.  Dereferencing a NULL pointer
is a segmentation fault in most cases; if you are used to getting "NULL"
as output from printf("%s\n", (char *)0), then you've been damaged by an
overly generous compiler or an overly protective OS.

Look at the definitions of OPT_ARG and OPT_ARG_SAFE in zsh.h.  You should
not be dereferencing ops->args directly, even in a debugging statement.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: trying dputs
  2014-11-20 17:05 ` Bart Schaefer
@ 2014-11-20 17:27   ` Ray Andrews
  0 siblings, 0 replies; 3+ messages in thread
From: Ray Andrews @ 2014-11-20 17:27 UTC (permalink / raw)
  To: zsh-workers

On 11/20/2014 09:05 AM, Bart Schaefer wrote:
> On Nov 19,  5:48pm, Ray Andrews wrote:
> }
> } If I steal 'opts->args' with a test string like this:
> }
> } //char *garbage = "utter garbage";
> } //ops->args = &garbage;
> }
> } ... I get what I expect from this:
> }
> } dputs("\nargs: %s", *(ops->args));
> }
> } ... but if I leave the pointer unmolested, I get a zsh
> } segmentation fault. Why is that?
>
> Because ops->args is a NULL pointer (not a pointer to an empty string)
> when there are no arguments to the option.  Dereferencing a NULL pointer
> is a segmentation fault in most cases; if you are used to getting "NULL"
> as output from printf("%s\n", (char *)0), then you've been damaged by an
> overly generous compiler or an overly protective OS.
If it's supposta be NULL when not in use, then I got what I should get, 
quite right.  I
just thought it might more safely point to a NULL string, but if that's 
not how it's
done, then it's probably for a good reason. I can't say otherwise.
>
> Look at the definitions of OPT_ARG and OPT_ARG_SAFE in zsh.h.  You should
> not be dereferencing ops->args directly, even in a debugging statement.
Ok.  Easy enough to test for a NULL pointer in any case.



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-11-20 17:27 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-20  1:48 trying dputs Ray Andrews
2014-11-20 17:05 ` Bart Schaefer
2014-11-20 17:27   ` Ray Andrews

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).