]> git.ipfire.org Git - thirdparty/libvirt.git/commit
virBufferAdd: Ensure that the buffer is initialized also when len == 0
authorPeter Krempa <pkrempa@redhat.com>
Thu, 4 Mar 2021 17:09:42 +0000 (18:09 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 5 Mar 2021 14:33:34 +0000 (15:33 +0100)
commit1553e7256701c725cf87bafa5d738da16f278c3d
treef2ad42410dde0bc6d2c06ce63fb394393cd4d740
parent9afc26502dc0b3b911e468cbaa0fc28f4d50c7a4
virBufferAdd: Ensure that the buffer is initialized also when len == 0

There's an optimization in virBufferAdd which returns early when the
length of the added string is 0 (given that auto-indent is disabled).

The optimization causes inconsistent behaviour between these two cases:

 virBufferAdd(buf, "", 0);  // this doesn't initialize the buffer

and

 virBufferAdd(buf, "", -1); //this initializes the buffer

Since using an empty string is used to prime the buffer to an empty
string it can be confusing. Remove the optimization.

This fixes such a wrong initialization done in x86FeatureNames.

Note that our code in many places expects that if no virBuffer APIs are
used on a buffer object, then NULL should be retured, so we can't always
prime the buffer to an empty string.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/util/virbuffer.c