Re: [libvirt] [PATCH v3 01/11] Add QMP probing for TPM

2013-04-01 Thread Corey Bryant



On 03/21/2013 11:42 AM, Stefan Berger wrote:
[snip]

+
+
+static int
+qemuMonitorJSONGetStringArray(qemuMonitorPtr mon, const char *qmpCmd,
+  char ***array)
+{
+int ret;
+virJSONValuePtr cmd;
+virJSONValuePtr reply = NULL;
+virJSONValuePtr data;
+char **list = NULL;
+int n = 0;
+size_t i;
+
+*array = NULL;
+
+if (!(cmd = qemuMonitorJSONMakeCommand(qmpCmd, NULL)))
+return -1;
+
+ret = qemuMonitorJSONCommand(mon, cmd, reply);
+
+if (ret == 0)
+ret = qemuMonitorJSONCheckError(cmd, reply);
+
+if (ret  0)
+goto cleanup;
+
+ret = -1;
+
+if (!(data = virJSONValueObjectGet(reply, return))) {
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _(%s reply was missing return data),
+   qmpCmd);
+goto cleanup;
+}
+
+if ((n = virJSONValueArraySize(data))  0) {
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _(%s reply data was not an array),
+   qmpCmd);
+goto cleanup;
+}
+
+/* null-terminated list */
+if (VIR_ALLOC_N(list, n + 1)  0) {
+virReportOOMError();
+goto cleanup;
+}
+
+for (i = 0 ; i  n ; i++) {
+virJSONValuePtr child = virJSONValueArrayGet(data, i);
+const char *tmp;
+
+if (!(tmp = virJSONValueGetString(child))) {
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _(%s array element does not contain data),
+   qmpCmd);
+goto cleanup;
+}
+
+if (!(list[i] = strdup(tmp))) {
+virReportOOMError();
+goto cleanup;
+}
+}
+
+ret = n;
+*array = list;
+
+cleanup:
+if (ret  0)
+virStringFreeList(list);
+virJSONValueFree(cmd);
+virJSONValueFree(reply);


Does data need to be freed?

--
Regards,
Corey Bryant


+return ret;
+}
+
+int qemuMonitorJSONGetTPMModels(qemuMonitorPtr mon,
+char ***tpmmodels)
+{
+return qemuMonitorJSONGetStringArray(mon, query-tpm-models, tpmmodels);
+}
+
+
+int qemuMonitorJSONGetTPMTypes(qemuMonitorPtr mon,
+   char ***tpmtypes)
+{
+return qemuMonitorJSONGetStringArray(mon, query-tpm-types, tpmtypes);
+}
Index: libvirt/src/qemu/qemu_monitor_json.h
===
--- libvirt.orig/src/qemu/qemu_monitor_json.h
+++ libvirt/src/qemu/qemu_monitor_json.h
@@ -341,4 +341,12 @@ int qemuMonitorJSONNBDServerAdd(qemuMoni
  const char *deviceID,
  bool writable);
  int qemuMonitorJSONNBDServerStop(qemuMonitorPtr mon);
+int qemuMonitorJSONGetTPMModels(qemuMonitorPtr mon,
+char ***tpmmodels)
+ATTRIBUTE_NONNULL(2);
+
+int qemuMonitorJSONGetTPMTypes(qemuMonitorPtr mon,
+   char ***tpmtypes)
+ATTRIBUTE_NONNULL(2);
+
  #endif /* QEMU_MONITOR_JSON_H */



--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v3 01/11] Add QMP probing for TPM

2013-04-01 Thread Stefan Berger

On 04/01/2013 03:45 PM, Corey Bryant wrote:

+if (ret  0)
+goto cleanup;
+
+ret = -1;
+
+if (!(data = virJSONValueObjectGet(reply, return))) {
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _(%s reply was missing return data),
+   qmpCmd);
+goto cleanup;
+}
+
+if ((n = virJSONValueArraySize(data))  0) {
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _(%s reply data was not an array),
+   qmpCmd);
+goto cleanup;
+}
+
+/* null-terminated list */
+if (VIR_ALLOC_N(list, n + 1)  0) {
+virReportOOMError();
+goto cleanup;
+}
+
+for (i = 0 ; i  n ; i++) {
+virJSONValuePtr child = virJSONValueArrayGet(data, i);
+const char *tmp;
+
+if (!(tmp = virJSONValueGetString(child))) {
+virReportError(VIR_ERR_INTERNAL_ERROR,
+   _(%s array element does not contain data),
+   qmpCmd);
+goto cleanup;
+}
+
+if (!(list[i] = strdup(tmp))) {
+virReportOOMError();
+goto cleanup;
+}
+}
+
+ret = n;
+*array = list;
+
+cleanup:
+if (ret  0)
+virStringFreeList(list);
+virJSONValueFree(cmd);
+virJSONValueFree(reply);

+

Does data need to be freed?



It doesn't seem to be the case for the 'return' object. No other code 
frees it, either.


   Stefan

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH v3 01/11] Add QMP probing for TPM

2013-04-01 Thread Eric Blake
On 04/01/2013 01:52 PM, Stefan Berger wrote:
 On 04/01/2013 03:45 PM, Corey Bryant wrote:
 +if (ret  0)
 +goto cleanup;
 +
 +ret = -1;
 +
 +if (!(data = virJSONValueObjectGet(reply, return))) {
 +virReportError(VIR_ERR_INTERNAL_ERROR,
 +   _(%s reply was missing return data),
 +   qmpCmd);
 +goto cleanup;
 +}
 +

 +cleanup:
 +if (ret  0)
 +virStringFreeList(list);
 +virJSONValueFree(cmd);
 +virJSONValueFree(reply);
 +

 Does data need to be freed?

 
 It doesn't seem to be the case for the 'return' object. No other code
 frees it, either.

data is a pointer to contents already within reply; freeing reply is
recursive, and explicitly freeing data in addition to reply would be a
double-free bug.  This aspect of the code is correct as written.

-- 
Eric Blake   eblake redhat com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list