
Jim Meyering wrote:
- if (VIR_ALLOC_N(*content, content_length) < 0 ) { + if (content_length > XEND_RCV_BUF_MAX_LEN) { + virXendError(VIR_ERR_INTERNAL_ERROR, + _("Xend returned HTTP Content-Length of %d, " + "which exceeds maximum of %d"), + content_length, + XEND_RCV_BUF_MAX_LEN); + return -1; + } +
This is subtle enough that a comment might avoid trouble later.
/* Allocate one byte beyond the end of the largest buffer we will read. Combined with the fact that VIR_ALLOC_N zeros the returned buffer, this guarantees that "content" will always be NUL-terminated. */
Good idea. I've added your wording verbatim.
+ if (VIR_ALLOC_N(*content, content_length + 1) < 0 ) { virReportOOMError(); return -1; } @@ -380,7 +390,7 @@ xend_get(virConnectPtr xend, const char *path, virXendError(VIR_ERR_GET_FAILED, _("%d status from xen daemon: %s:%s"), ret, path, - content ? *content: "NULL"); + content ? *content : "NULL");
Oh! I've just noticed that testing "content" is wrong, since it will always be non-NULL. BTW, the parameter should be marked as such via "ATTRIBUTE_NONNULL (3)".
I added ATTRIBUTE_NONNULL to xend_req() as well.
The test should examine "*content", i.e.,
*content ? *content : "NULL");
Then I remembered the NULLSTR macro. You can replace the above with this:
NULLSTR(*content));
FYI, here's its definition:
$ git grep -A2 ine.NULLSTR src/internal.h:# define NULLSTR(s) \ src/internal.h- ((void)verify_true(sizeof *(s) == sizeof (char)), \ src/internal.h- (s) ? (s) : "(null)")
Cool, that's handy. Updated patch attached. Regards, Jim