Discussion:
sscanf in conditional - what should happen?
Chris Angelico
2015-01-18 10:26:45 UTC
Permalink
Is this meant to be legal?

if (argv[1]=="#zero" || sscanf(argv[1],"#%d",int x))
{
... code using x ...
}

If the first part of the condition is true, x ought to be UNDEFINED. I
had code conceptually similar to this, but in the case where the first
condition held, x inexplicably had a completely different object
(probably the top of the stack; it was a Stdio.Stat object that I'd
used elsewhere). But am I trying to depend on unreasonable code here?

Breaking out the declaration to the line above solves the problem. I'm
just curious as to whether this ought to work or is pure chance.

ChrisA
Arne Goedeke
2015-01-18 12:40:31 UTC
Permalink
Yes, it ought to be undefined. Can you produce a testcase?

arne

Maybe we can
Post by Chris Angelico
Is this meant to be legal?
if (argv[1]=="#zero" || sscanf(argv[1],"#%d",int x))
{
... code using x ...
}
If the first part of the condition is true, x ought to be UNDEFINED. I
had code conceptually similar to this, but in the case where the first
condition held, x inexplicably had a completely different object
(probably the top of the stack; it was a Stdio.Stat object that I'd
used elsewhere). But am I trying to depend on unreasonable code here?
Breaking out the declaration to the line above solves the problem. I'm
just curious as to whether this ought to work or is pure chance.
ChrisA
Chris Angelico
2015-01-18 14:06:02 UTC
Permalink
Post by Arne Goedeke
Yes, it ought to be undefined. Can you produce a testcase?
This is about as minimal as I can get it.

int main()
{
string ext=".rar";
{
string foo="oops!"; //This ends up in x
}
if (ext==".rar" || sscanf(ext,".r%d",int x))
write("x should be an integer, is actually %O\n",x);
}

On a current Pike 8.1, this has "oops!" in x. Likewise in 7.8.866 on Windows.

ChrisA
Arne Goedeke
2015-01-31 13:00:22 UTC
Permalink
I took the liberty to add this as a testcase. I think the most
reasonable behavior would be to initialize the variables to being
UNDEFINED, regardless of the call to sscanf. I don't know how that could
be done in the compiler.

arne
Post by Chris Angelico
Post by Arne Goedeke
Yes, it ought to be undefined. Can you produce a testcase?
This is about as minimal as I can get it.
int main()
{
string ext=".rar";
{
string foo="oops!"; //This ends up in x
}
if (ext==".rar" || sscanf(ext,".r%d",int x))
write("x should be an integer, is actually %O\n",x);
}
On a current Pike 8.1, this has "oops!" in x. Likewise in 7.8.866 on Windows.
ChrisA
Loading...