diff options
author | ukyg9e5r6k7gubiekd6 <ukyg9e5r6k7gubiekd6@mailinator.com> | 2012-08-06 21:10:39 +1000 |
---|---|---|
committer | Eric S. Raymond <esr@thyrsus.com> | 2012-09-07 05:17:38 -0400 |
commit | a21fca3ec713bc5e2c69258ffc6bc4b95014476a (patch) | |
tree | aef3b13f9fe76efadd12f41320b0421ef3df8e69 | |
parent | 6386d5265bb1df4d3b94d6f49f584da31f9714a1 (diff) | |
download | gpsd-a21fca3ec713bc5e2c69258ffc6bc4b95014476a.tar.gz |
Comply strictly with strict-aliasing rules by using memcpy...
...rather than using type-punning pointer casts. With optimization
this will generate similarly-performing or even identical code on most
architectures/compilers.
Signed-off-by: Eric S. Raymond <esr@thyrsus.com>
-rw-r--r-- | json.c | 33 |
1 files changed, 24 insertions, 9 deletions
@@ -193,14 +193,14 @@ static int json_internal_read_object(const char *cp, if (lptr != NULL) switch (cursor->type) { case t_integer: - *((int *)lptr) = cursor->dflt.integer; + memcpy(lptr, &cursor->dflt.integer, sizeof(int)); break; case t_uinteger: - *((unsigned int *)lptr) = cursor->dflt.uinteger; + memcpy(lptr, &cursor->dflt.uinteger, sizeof(unsigned int)); break; case t_time: case t_real: - *((double *)lptr) = cursor->dflt.real; + memcpy(lptr, &cursor->dflt.real, sizeof(double)); break; case t_string: if (parent != NULL @@ -210,7 +210,7 @@ static int json_internal_read_object(const char *cp, lptr[0] = '\0'; break; case t_boolean: - *((bool *) lptr) = cursor->dflt.boolean; + memcpy(lptr, &cursor->dflt.boolean, sizeof(bool)); break; case t_character: lptr[0] = cursor->dflt.character; @@ -439,16 +439,28 @@ static int json_internal_read_object(const char *cp, if (lptr != NULL) switch (cursor->type) { case t_integer: - *((int *)lptr) = atoi(valbuf); + { + int tmp = atoi(valbuf); + memcpy(lptr, &tmp, sizeof(int)); + } break; case t_uinteger: - *((unsigned int *)lptr) = (unsigned)atoi(valbuf); + { + unsigned int tmp = (unsigned int)atoi(valbuf); + memcpy(lptr, &tmp, sizeof(unsigned int)); + } break; case t_time: - *((double *)lptr) = iso8601_to_unix(valbuf); + { + double tmp = iso8601_to_unix(valbuf); + memcpy(lptr, &tmp, sizeof(double)); + } break; case t_real: - *((double *)lptr) = safe_atof(valbuf); + { + double tmp = safe_atof(valbuf); + memcpy(lptr, &tmp, sizeof(double)); + } break; case t_string: if (parent != NULL @@ -458,7 +470,10 @@ static int json_internal_read_object(const char *cp, (void)strlcpy(lptr, valbuf, cursor->len); break; case t_boolean: - *((bool *) lptr) = (strcmp(valbuf, "true") == 0); + { + bool tmp = (strcmp(valbuf, "true") == 0); + memcpy(lptr, &tmp, sizeof(bool)); + } break; case t_character: if (strlen(valbuf) > 1) |