radcheb / Adhan

High precision prayer time library
MIT License
13 stars 4 forks source link

use of the memory in a embedded system #9

Closed Cyrillius closed 7 years ago

Cyrillius commented 7 years ago

@radcheb This kind of methods is a little bit dangerous for the integrity of the memory...

This method will allocate dynamically some space for the solar_time_t struct and we are not sure where the allocated memory will be freed

solar_time_t * new_solar_time(const time_t today_time, coordinates_t *coordinates) {

    ...

    solar_time_t *result = malloc(sizeof(solar_time_t));
    *(double *) &result->transit = transit;
    *(double *) &result->sunrise = sunrise;
    *(double *) &result->sunset = sunset;
    *(coordinates_t **) &result->observer = observer;
    *(solar_coordinates_t **) &result->solar = solar;
    *(solar_coordinates_t **) &result->prevSolar = prevSolar;
    *(solar_coordinates_t **) &result->nextSolar = nextSolar;
    *(double *) &result->approximateTransit = approximateTransit;

    return result;
}

I suggest you to do something like this instead:

void new_solar_time(const time_t today_time, coordinates_t *coordinates, solar_time_t * solar_time) {
    ...

    *(double *) &result->transit = transit;
    *(double *) &result->sunrise = sunrise;
    *(double *) &result->sunset = sunset;
    *(coordinates_t **) &result->observer = observer;
    *(solar_coordinates_t **) &result->solar = solar;
    *(solar_coordinates_t **) &result->prevSolar = prevSolar;
    *(solar_coordinates_t **) &result->nextSolar = nextSolar;
    *(double *) &result->approximateTransit = approximateTransit;
}
radcheb commented 7 years ago

I replaced most pointer-based references by direct references: 61276f93e548170d5365d61b90557a11f556d3e7 4f3829fa716f6e33d07ea7f45cdd6774fdecfaf9 9f274c0f3ee80b997d01d521cf16988a3dfd8b42 bf4bb750661d0dbcfa49a8526aee8b969b571469 d9ab19c08e1427a2f29d1e76565dd4735d40edaf ab131c5d68c011ecbe39faef957e73f290e5dd0a Still one malloc in prayer_times, we can remove it also if you want.

Cyrillius commented 7 years ago

If you can. It will be great to have no malloc