On Mon, Apr 03, 2023 at 01:19:12PM -0700, Stephen Hemminger wrote: > On Mon, 3 Apr 2023 09:30:23 -0700 > Tyler Retzlaff <roret...@linux.microsoft.com> wrote: > > > __json_snprintf(char *buf, const int len, const char *format, ...) > > { > > - char tmp[len]; > > + char *tmp = malloc(len); > > va_list ap; > > - int ret; > > + int ret = 0; > > + > > + if (tmp == NULL) > > + return ret; > > > > va_start(ap, format); > > ret = vsnprintf(tmp, sizeof(tmp), format, ap); > > va_end(ap); > > if (ret > 0 && ret < (int)sizeof(tmp) && ret < len) { > > strcpy(buf, tmp); > > - return ret; > > } > > - return 0; /* nothing written or modified */ > > + > > + free(tmp); > > + > > + return ret; > > } > > Not sure why it needs a tmp buffer anyway?
The temporary buffer is to ensure that in the case that the data doesn't fit in the buffer, the buffer remains unmodified. The reason for this is that when building up the json response we always have a valid json string. For example, suppose we are preparing a response with an array of two strings. After the first string has been processed, the output buffer contains: '["string1"]'. When json_snprintf is being called to add string2, there are a couple of things to note: * the text to be inserted will be put not at the end of the string, but before the closing "]". * the actual text to be inserted will be ',"string2"]', so ensuring that the final buffer is valid. However, the error case is problematic. While we can catch the case where the string to be inserted overflows/has been truncated, doing a regular snprintf means that our output buffer could contain invalid json, as our end-terminator would have been overwritten, e.g. '["string1","string2' To guarantee the output from telemetry is always valid json, even in case of truncation, we use a temporary buffer to do the write initially, and if it doesn't get truncated, we then copy that to the final buffer. That's the logic for this temporary buffer. Now, thinking about it yesterday evening, there are other ways in which we can do this, which can avoid this temporary buffer. 1. We can do the initial snprintf to an empty buffer to get the length that way. This will still be slower, as it means that we need to do printf processing twice rather than using memcpy to copy the result. However, it's probably less overhead than malloc and free. 2. AFAIK, the normal case for this function being called is with a single terminator at the end of the string. We can take advantage of that, by checking if the '\0' just one character into the string we are printing, and, if so, to store that once character. If we have a snprintf error leading to truncation, it then allows us to restore the original string. My suggestion is to use a combination of these methods. In json_snprintf check if the input buffer is empty or has only one character in it, and use method #2 if so. If that's not the case, then fallback to method #1 and do a double snprintf. Make sense? Any other suggestions? /Bruce