diff options
| author | 2014-05-23 11:01:57 +0200 | |
|---|---|---|
| committer | 2014-05-23 11:01:57 +0200 | |
| commit | 6c0bf73f0773873edf8754246c291235aa217e7a (patch) | |
| tree | c8b3023f702ff98cb92cb437ed65b37a7e6b1797 /src/xplist.c | |
| parent | 8ac53f53b60e0ac22b4f41f67c3f1f0526e1abb2 (diff) | |
| download | libplist-6c0bf73f0773873edf8754246c291235aa217e7a.tar.gz libplist-6c0bf73f0773873edf8754246c291235aa217e7a.tar.bz2 | |
Handle signed vs. unsigned integer values correctly
Diffstat (limited to 'src/xplist.c')
| -rw-r--r-- | src/xplist.c | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/src/xplist.c b/src/xplist.c index d953e23..eaa2468 100644 --- a/src/xplist.c +++ b/src/xplist.c | |||
| @@ -184,7 +184,11 @@ static void node_to_xml(node_t* node, void *xml_struct) | |||
| 184 | case PLIST_UINT: | 184 | case PLIST_UINT: |
| 185 | tag = XPLIST_INT; | 185 | tag = XPLIST_INT; |
| 186 | val = (char*)malloc(64); | 186 | val = (char*)malloc(64); |
| 187 | (void)snprintf(val, 64, "%"PRIu64, node_data->intval); | 187 | if (node_data->length == 16) { |
| 188 | (void)snprintf(val, 64, "%"PRIu64, node_data->intval); | ||
| 189 | } else { | ||
| 190 | (void)snprintf(val, 64, "%"PRIi64, node_data->intval); | ||
| 191 | } | ||
| 188 | break; | 192 | break; |
| 189 | 193 | ||
| 190 | case PLIST_REAL: | 194 | case PLIST_REAL: |
| @@ -377,9 +381,30 @@ static void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node) | |||
| 377 | if (!xmlStrcmp(node->name, XPLIST_INT)) | 381 | if (!xmlStrcmp(node->name, XPLIST_INT)) |
| 378 | { | 382 | { |
| 379 | xmlChar *strval = xmlNodeGetContent(node); | 383 | xmlChar *strval = xmlNodeGetContent(node); |
| 380 | data->intval = strtoull((char*)strval, NULL, 0); | 384 | int is_negative = 0; |
| 385 | char *str = (char*)strval; | ||
| 386 | if ((str[0] == '-') || (str[0] == '+')) { | ||
| 387 | if (str[0] == '-') { | ||
| 388 | is_negative = 1; | ||
| 389 | } | ||
| 390 | str++; | ||
| 391 | } | ||
| 392 | char* endp = NULL; | ||
| 393 | data->intval = strtoull((char*)str, &endp, 0); | ||
| 394 | if ((endp != NULL) && (strlen(endp) > 0)) { | ||
| 395 | fprintf(stderr, "%s: integer parse error: string contains invalid characters: '%s'\n", __func__, endp); | ||
| 396 | } | ||
| 397 | if (is_negative || (data->intval <= INT64_MAX)) { | ||
| 398 | int64_t v = data->intval; | ||
| 399 | if (is_negative) { | ||
| 400 | v = -v; | ||
| 401 | } | ||
| 402 | data->intval = (uint64_t)v; | ||
| 403 | data->length = 8; | ||
| 404 | } else { | ||
| 405 | data->length = 16; | ||
| 406 | } | ||
| 381 | data->type = PLIST_UINT; | 407 | data->type = PLIST_UINT; |
| 382 | data->length = 8; | ||
| 383 | xmlFree(strval); | 408 | xmlFree(strval); |
| 384 | continue; | 409 | continue; |
| 385 | } | 410 | } |
