The next() function is broken for any TFTP packet with padding
which doesn't end with a zero.
Rewrite to handle such packets.
Thanks to Helge Deller <deller@gmx.de> for persisting in finding the
actual problem and proposing a solution. This patch is modelled on his,
but rewritten for personal preference by Simon Kelley, who is
responsible for all bugs.
}
p = packet + 2;
- end = packet + 2 + len;
+ end = packet + len;
if (ntohs(*((unsigned short *)packet)) != OP_RRQ ||
!(filename = next(&p, end)) ||
static char *next(char **p, char *end)
{
- char *ret = *p;
- size_t len;
+ char *n, *ret = *p;
+
+ /* Look for end of string, without running off the end of the packet. */
+ for (n = *p; n < end && *n != 0; n++);
- if (*(end-1) != 0 ||
- *p == end ||
- (len = strlen(ret)) == 0)
+ /* ran off the end or zero length string - failed */
+ if (n == end || n == ret)
return NULL;
-
- *p += len + 1;
+
+ *p = n + 1;
return ret;
}