X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/d0912d1f5b4c65b0be0c3fba2a264a1cfbf96d08..f33ba69e92f389f5884eae5a586858a629a5260b:/misc.c diff --git a/misc.c b/misc.c index 8edb41a8..50c38d57 100644 --- a/misc.c +++ b/misc.c @@ -1,3 +1,7 @@ +/* + * Platform-independent routines shared between all PuTTY programs. + */ + #include #include #include @@ -11,9 +15,12 @@ char *dupstr(const char *s) { - int len = strlen(s); - char *p = smalloc(len + 1); - strcpy(p, s); + char *p = NULL; + if (s) { + int len = strlen(s); + p = snewn(len + 1, char); + strcpy(p, s); + } return p; } @@ -34,7 +41,7 @@ char *dupcat(const char *s1, ...) } va_end(ap); - p = smalloc(len + 1); + p = snewn(len + 1, char); strcpy(p, s1); q = p + strlen(p); @@ -54,13 +61,46 @@ char *dupcat(const char *s1, ...) /* * Do an sprintf(), but into a custom-allocated buffer. * - * Irritatingly, we don't seem to be able to do this portably using - * vsnprintf(), because there appear to be issues with re-using the - * same va_list for two calls, and the excellent C99 va_copy is not - * yet widespread. Bah. Instead I'm going to do a horrid, horrid - * hack, in which I trawl the format string myself, work out the - * maximum length of each format component, and resize the buffer - * before printing it. + * Currently I'm doing this via vsnprintf. This has worked so far, + * but it's not good, because: + * + * - vsnprintf is not available on all platforms. There's an ifdef + * to use `_vsnprintf', which seems to be the local name for it + * on Windows. Other platforms may lack it completely, in which + * case it'll be time to rewrite this function in a totally + * different way. + * + * - technically you can't reuse a va_list like this: it is left + * unspecified whether advancing a va_list pointer modifies its + * value or something it points to, so on some platforms calling + * vsnprintf twice on the same va_list might fail hideously. It + * would be better to use the `va_copy' macro mandated by C99, + * but that too is not yet ubiquitous. + * + * The only `properly' portable solution I can think of is to + * implement my own format string scanner, which figures out an + * upper bound for the length of each formatting directive, + * allocates the buffer as it goes along, and calls sprintf() to + * actually process each directive. If I ever need to actually do + * this, some caveats: + * + * - It's very hard to find a reliable upper bound for + * floating-point values. %f, in particular, when supplied with + * a number near to the upper or lower limit of representable + * numbers, could easily take several hundred characters. It's + * probably feasible to predict this statically using the + * constants in , or even to predict it dynamically by + * looking at the exponent of the specific float provided, but + * it won't be fun. + * + * - Don't forget to _check_, after calling sprintf, that it's + * used at most the amount of space we had available. + * + * - Fault any formatting directive we don't fully understand. The + * aim here is to _guarantee_ that we never overflow the buffer, + * because this is a security-critical function. If we see a + * directive we don't know about, we should panic and die rather + * than run any risk. */ char *dupprintf(const char *fmt, ...) { @@ -76,7 +116,7 @@ char *dupvprintf(const char *fmt, va_list ap) char *buf; int len, size; - buf = smalloc(512); + buf = snewn(512, char); size = 512; while (1) { @@ -97,7 +137,7 @@ char *dupvprintf(const char *fmt, va_list ap) * buffer wasn't big enough, so we enlarge it a bit and hope. */ size += 512; } - buf = srealloc(buf, size); + buf = sresize(buf, size, char); } } @@ -178,6 +218,8 @@ void bufchain_add(bufchain *ch, const void *data, int len) { const char *buf = (const char *)data; + if (len == 0) return; + ch->buffersize += len; if (ch->tail && ch->tail->buflen < BUFFER_GRANULE) { @@ -190,7 +232,7 @@ void bufchain_add(bufchain *ch, const void *data, int len) while (len > 0) { int grainlen = min(len, BUFFER_GRANULE); struct bufchain_granule *newbuf; - newbuf = smalloc(sizeof(struct bufchain_granule)); + newbuf = snew(struct bufchain_granule); newbuf->bufpos = 0; newbuf->buflen = grainlen; memcpy(newbuf->buf, buf, grainlen);