From 8e25669c024ec0e83f3916c027e629500d49d9a8 Mon Sep 17 00:00:00 2001 From: Attila Kovacs Date: Mon, 10 Feb 2025 12:26:42 -0500 Subject: [PATCH] More portable starname / catalog name size checking. --- CHANGELOG.md | 3 +++ src/novas.c | 28 ++++++++++++++-------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d024501f..8f2e8631 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -91,6 +91,9 @@ such as for handling times and angles as strings, rise and set times, and other - `julian_date()` and `cal_date()` now use astronomical calendar dates instead of the fixed Gregorian dates of before. Astronomical dates are Julian/Roman calendar dates prior to the Gregorian calendar reform of 1582. + - Use `SIZE_OF_OBJ_NAME` and `SIZE_OF_CAT_NAME` instead of `sizeof(obj->starname)` and `sizeof(obj->catalog)` + internally for improved portability. + - Updated `README.md` for v1.3 and benchmarks, including comparisons to __astropy__. diff --git a/src/novas.c b/src/novas.c index 6fb5d6f8..7db9f0b7 100644 --- a/src/novas.c +++ b/src/novas.c @@ -6319,8 +6319,8 @@ short transform_cat(enum novas_transform_type option, double jd_tt_in, const cat if(!in || !out) return novas_error(-1, EINVAL, fn, "NULL parameter: in=%p, out=%p", in, out); - if(out_id && strlen(out_id) >= sizeof(out->starname)) - return novas_error(2, EINVAL, fn, "output catalog ID is too long (%d > %d)", (int) strlen(out_id), (int) sizeof(out->starname) - 1); + if(out_id && strlen(out_id) >= SIZE_OF_OBJ_NAME) + return novas_error(2, EINVAL, fn, "output catalog ID is too long (%d > %d)", (int) strlen(out_id), SIZE_OF_OBJ_NAME - 1); if(option == CHANGE_J2000_TO_ICRS || option == CHANGE_ICRS_TO_J2000) { // ICRS frame ties always assume J2000 for both input and output... @@ -6425,19 +6425,19 @@ short transform_cat(enum novas_transform_type option, double jd_tt_in, const cat // Set the catalog identification code for the transformed catalog entry. if(out_id) - strncpy(out->catalog, out_id, sizeof(out->catalog)); + strncpy(out->catalog, out_id, SIZE_OF_CAT_NAME); else if(out != in) - strncpy(out->catalog, in->catalog, sizeof(out->catalog)); + strncpy(out->catalog, in->catalog, SIZE_OF_CAT_NAME); if(out != in) { // Copy unchanged quantities from the input catalog entry to the transformed catalog entry. - strncpy(out->starname, in->starname, sizeof(out->starname)); - out->starname[sizeof(out->starname) - 1] = '\0'; + strncpy(out->starname, in->starname, SIZE_OF_OBJ_NAME); + out->starname[SIZE_OF_OBJ_NAME - 1] = '\0'; out->starnumber = in->starnumber; } - // Make sure ctaalog name is terminated. - out->catalog[sizeof(out->catalog) - 1] = '\0'; + // Make sure catalog name is terminated. + out->catalog[SIZE_OF_CAT_NAME - 1] = '\0'; return 0; } @@ -6966,15 +6966,15 @@ short make_cat_entry(const char *star_name, const char *catalog, long cat_num, d // Set up the 'star' structure. if(star_name) { - if(strlen(star_name) >= sizeof(star->starname)) - return novas_error(1, EINVAL, fn, "Input star_name is too long (%d > %d)", (int) strlen(star_name), (int) sizeof(star->starname) - 1); - strncpy(star->starname, star_name, sizeof(star->starname) - 1); + if(strlen(star_name) >= SIZE_OF_OBJ_NAME) + return novas_error(1, EINVAL, fn, "Input star_name is too long (%d > %d)", (int) strlen(star_name), SIZE_OF_OBJ_NAME - 1); + strncpy(star->starname, star_name, SIZE_OF_OBJ_NAME - 1); } if(catalog) { - if(strlen(catalog) >= sizeof(star->catalog)) - return novas_error(2, EINVAL, fn, "Input cataog ID is too long (%d > %d)", (int) strlen(catalog), (int) sizeof(star->catalog) - 1); - strncpy(star->catalog, catalog, sizeof(star->catalog) - 1); + if(strlen(catalog) >= SIZE_OF_CAT_NAME) + return novas_error(2, EINVAL, fn, "Input cataog ID is too long (%d > %d)", (int) strlen(catalog), SIZE_OF_CAT_NAME - 1); + strncpy(star->catalog, catalog, SIZE_OF_CAT_NAME - 1); } star->starnumber = cat_num;