add buffer size check in find_calibration

Drop find_calibration_by_model function that was occassionally,
as I suppose, marked not static.
This commit is contained in:
Egor Shestakov
2025-03-12 12:16:04 +00:00
parent 9b1ee0161d
commit e02b54ec57
3 changed files with 8 additions and 15 deletions

View File

@@ -12,8 +12,12 @@
#endif #endif
bool bool
find_calibration_by_model(char *conffile, char *model, const char *sensor) find_calibration(size_t size, char *conffile, const char *sensor)
{ {
char model[512];
int model_found = libmegapixels_get_model(sizeof(model), model);
if(!model_found)
return false;
static const char *paths[] = { static const char *paths[] = {
"config/%s,%s.dcp", "config/%s,%s.dcp",
SYSCONFDIR "/megapixels/config/%s,%s.dcp", SYSCONFDIR "/megapixels/config/%s,%s.dcp",
@@ -21,27 +25,16 @@ find_calibration_by_model(char *conffile, char *model, const char *sensor)
NULL NULL
}; };
for (const char *fmt = paths[0]; fmt; fmt++) { for (const char *fmt = paths[0]; fmt; fmt++) {
sprintf(conffile, fmt, model, sensor); snprintf(conffile, size, fmt, model, sensor);
if (access(conffile, F_OK) != -1) { if (access(conffile, F_OK) != -1) {
printf("Found calibration file at %s\n", conffile); printf("Found calibration file at %s\n", conffile);
return true; return true;
} }
} }
printf("No calibration found for %s,%s\n", model, sensor); printf("No calibration found for %s,%s\n", model, sensor);
return false; return false;
} }
bool
find_calibration(char *conffile, const char *sensor)
{
char model[512];
int model_found = libmegapixels_get_model(sizeof(model), model);
if (!model_found)
return false;
return find_calibration_by_model(conffile, model, sensor);
}
struct MPCameraCalibration struct MPCameraCalibration
parse_calibration_file(const char *path) parse_calibration_file(const char *path)
{ {

View File

@@ -19,4 +19,4 @@ struct MPCameraCalibration {
struct MPCameraCalibration parse_calibration_file(const char *path); struct MPCameraCalibration parse_calibration_file(const char *path);
bool find_calibration(char *conffile, const char *sensor); bool find_calibration(size_t size, char *conffile, const char *sensor);

View File

@@ -1567,7 +1567,7 @@ update_state(MPPipeline *pipeline, const mp_state_proc *new_state)
if (camera_changed) { if (camera_changed) {
char cf[PATH_MAX]; char cf[PATH_MAX];
if (find_calibration(cf, state_proc.camera->name)) { if (find_calibration(sizeof(cf), cf, state_proc.camera->name)) {
state_proc.calibration = parse_calibration_file(cf); state_proc.calibration = parse_calibration_file(cf);
} else { } else {
fprintf(stderr, "No calibration for %s\n", state_proc.camera->name); fprintf(stderr, "No calibration for %s\n", state_proc.camera->name);