Skip to content

Commit

Permalink
make_object() to remain safe for objects compiled with older version of
Browse files Browse the repository at this point in the history
the `object` structure.
  • Loading branch information
attipaci committed Nov 17, 2024
1 parent 3bb173d commit c04b6b0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/novas.h
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ typedef struct {
long number; ///< enum novas_planet, or minor planet ID (e.g. NAIF), or star catalog ID.
char name[SIZE_OF_OBJ_NAME]; ///< name of the object (0-terminated)
cat_entry star; ///< basic astrometric data for NOVAS_CATALOG_OBJECT type.
novas_orbital_elements orbit; ///< orbital data for NOVAS_ORBITAL_OBJECT TYPE. @since 1.2
novas_orbital_elements orbit; ///< orbital data for NOVAS_ORBITAL_OBJECT type. @since 1.2
} object;

/**
Expand Down
15 changes: 14 additions & 1 deletion src/novas.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <errno.h>
#include <unistd.h>
#include <stdarg.h> // stdarg.h before stdio.h (for older gcc...)
#include <stddef.h>

#if !COMPAT
# include <stdio.h>
Expand Down Expand Up @@ -6715,6 +6716,13 @@ void novas_case_sensitive(int value) {
* compatibility with NOVAS C) source names are converted to upper-case internally. You can
* however enable case-sensitive processing by calling novas_case_sensitive() before.
*
* NOTES:
* <ol>
* <li>This call does not initialize the `orbit` field (added in v1.2) with zeroes to remain ABI
* compatible with versions &lt;1.2, and to avoid the possiblity of segfaulting if used to
* initialize a legacy `object` variable.</li>
* </ol>
*
* @param type The type of object. NOVAS_PLANET (0), NOVAS_EPHEM_OBJECT (1) or
* NOVAS_CATALOG_OBJECT (2), or NOVAS_ORBITAL_OBJECT (3).
* @param number The novas ID number (for solar-system bodies only, otherwise ignored)
Expand Down Expand Up @@ -6746,7 +6754,8 @@ short make_object(enum novas_object_type type, long number, const char *name, co
if(!source)
return novas_error(-1, EINVAL, fn, "NULL input source");

memset(source, 0, sizeof(*source));
// FIXME for version v2.x initialize the entire structure again...
memset(source, 0, offsetof(object, orbit));

// Set the object type.
if(type < 0 || type >= NOVAS_OBJECT_TYPES)
Expand All @@ -6759,6 +6768,10 @@ short make_object(enum novas_object_type type, long number, const char *name, co
if(number < 0 || number >= NOVAS_PLANETS)
return novas_error(2, EINVAL, fn, "planet number %ld is out of bounds [0:%d]", number, NOVAS_PLANETS - 1);

// FIXME will not need special case in v2.x
if(type == NOVAS_ORBITAL_OBJECT)
memset(&source->orbit, 0, sizeof(source->orbit));

source->number = number;

if(name) {
Expand Down

0 comments on commit c04b6b0

Please sign in to comment.