Re:The Linux Backdoor Attempt of 2003 (2013)
by mhh__ on Monday August 10, 2020 @11:40AM
(#24110455)
It should really be forbidden by the compiler these days, or at least a very loud warning.
[ Parent
| on HN ]
Re:The Linux Backdoor Attempt of 2003 (2013)
by GuB-42 on Monday August 10, 2020 @12:10PM
(#24110777)
It would break these kinds of constructs, which are common.
if ((fd = open(...)) != -1) {
/* do something with fd */
} else {
perror("open");
}
The compiler outputs a warning when you have something like "if (a = b)". If that's what you mean (it sometimes is), you have to write it "if ((a = b))" to silence the warning.
Re:The Linux Backdoor Attempt of 2003 (2013)
by a1369209993 on Monday August 10, 2020 @03:34PM
(#24113134)
TBF, that doesn't actually work - you have to write:
int fd; /* fd must be declared in order to be assigned */
if ((fd = open(...)) != -1) {
/* do something with fd */
} else {
perror("open");
}
which would be better written as: int fd = open(...);
if (fd != -1) /* etc */
It would be nice for scoping reasons to be able to write something like: if ((int fd = open(...)) != -1) /* etc */
but if ((int fd == open(...)) != -1) /* etc */
isn't valid code.
Re:The Linux Backdoor Attempt of 2003 (2013)
by GuB-42 on Monday August 10, 2020 @05:19PM
(#24114118)
Exactly my thoughts, I was about to comment on that but I was too lazy so I omitted the declaration. Obviously, this is HN, so someone had to point it out ;)
Anyways, I am a bit torn about the second option. I like the idea of putting the call inside the if clause as it makes for a very explicit error handling but the uninitialized declaration is ugly. What I do in practice tends to depend on the situation but it is rarely satisfying.
Your last suggestion would be ideal, but as you said, it is invalid code unfortunately.
Maybe this
for (int fd = open(...); fd != -1; fd = -1) {
/* do stuff */
}
Just kidding, don't do that.
Re:The Linux Backdoor Attempt of 2003 (2013)
by a1369209993 on Monday August 10, 2020 @11:08PM
(#24116788)
> for (int fd = open(...); fd != -1; fd = -1) {
that doesn't correctly (or rather at all) handle the failure case. You should do (IIRC): #define LET(...) for(__VA_ARGS__,_tmp[0],*_once=_tmp; _once ;_once=0)
/* ^^^ goes in a header file */
LET(int fd = open())
{
if(fd == -1) { /* handle error, return or break */ }
/* do stuff */
}
Re:The Linux Backdoor Attempt of 2003 (2013)
by saagarjha on Monday August 10, 2020 @08:08PM
(#24115754)
C++17 has this! See https://en.cppreference.com/w/cpp/language/if.
Re:The Linux Backdoor Attempt of 2003 (2013)
by saagarjha on Monday August 10, 2020 @08:10PM
(#24115777)
Here's a classic:
char *strcpy(char *d, char *s) {
char *r = d;
while (*d++ = *s++);
return r;
}
(If you don't need the return value, this becomes even nicer.)
Re:The Linux Backdoor Attempt of 2003 (2013)
by djeiasbsbo on Monday August 10, 2020 @02:57PM
(#24112738)
What about:
while((i = getchar()) != EOF) putchar(i);
This type of thing seems to be "encouraged"...