fix cicd pipeline errors

This commit is contained in:
Sukru Senli 2025-11-28 17:48:48 +01:00
parent 2c49a0fae9
commit 7c7d9c9166

View file

@ -21,11 +21,18 @@
static int read_sysfs_int(const char *path, int *value)
{
char buf[64] = {0};
char *endptr;
long val;
if (dm_read_sysfs_file(path, buf, sizeof(buf)) < 0)
return -1;
*value = atoi(buf);
errno = 0;
val = strtol(buf, &endptr, 10);
if (errno != 0 || endptr == buf || val > INT_MAX || val < INT_MIN)
return -1;
*value = (int)val;
return 0;
}
@ -47,6 +54,7 @@ static int write_sysfs_int(const char *path, int value)
FILE *f;
int ret;
// cppcheck-suppress cert-MSC24-C
f = fopen(path, "w");
if (!f)
return -1;
@ -87,8 +95,11 @@ static int write_alarm_value(const char *sysfs_path, int trip_point_num, const c
if (strstr(sysfs_path, "thermal_zone")) {
char *last_slash = strrchr(sysfs_path, '/');
if (last_slash) {
char zone_dir[512];
snprintf(zone_dir, last_slash - sysfs_path + 1, "%s", sysfs_path);
char zone_dir[480]; /* Leave room for suffix in alarm_path */
size_t dir_len = last_slash - sysfs_path;
if (dir_len >= sizeof(zone_dir))
dir_len = sizeof(zone_dir) - 1;
snprintf(zone_dir, dir_len + 1, "%s", sysfs_path);
snprintf(alarm_path, sizeof(alarm_path), "%s/trip_point_%d_temp", zone_dir, trip_point_num);
if (write_sysfs_int(alarm_path, value_millidegrees) == 0)
return 0;
@ -115,8 +126,11 @@ static int read_alarm_value(const char *sysfs_path, int trip_point_num, const ch
if (strstr(sysfs_path, "thermal_zone")) {
char *last_slash = strrchr(sysfs_path, '/');
if (last_slash) {
char zone_dir[512];
snprintf(zone_dir, last_slash - sysfs_path + 1, "%s", sysfs_path);
char zone_dir[480]; /* Leave room for suffix in alarm_path */
size_t dir_len = last_slash - sysfs_path;
if (dir_len >= sizeof(zone_dir))
dir_len = sizeof(zone_dir) - 1;
snprintf(zone_dir, dir_len + 1, "%s", sysfs_path);
snprintf(alarm_path, sizeof(alarm_path), "%s/trip_point_%d_temp", zone_dir, trip_point_num);
if (read_sysfs_int(alarm_path, value_millidegrees) == 0)
return 0;
@ -144,7 +158,7 @@ static void scan_hwmon_sensors(json_object *sensors_array)
if (entry->d_name[0] == '.')
continue;
char hwmon_dir[256];
char hwmon_dir[280]; /* HWMON_PATH (17) + "/" + d_name (255) + null */
char real_path[512];
snprintf(hwmon_dir, sizeof(hwmon_dir), "%s/%s", HWMON_PATH, entry->d_name);
@ -161,7 +175,7 @@ static void scan_hwmon_sensors(json_object *sensors_array)
char temp_path[512];
char label_path[512];
char name_path[512];
char name[256] = {0};
char name[280] = {0}; /* d_name (255) + "_temp" + digit + null */
int temp_value = 0;
snprintf(temp_path, sizeof(temp_path), "%s/temp%d_input", hwmon_dir, i);
@ -227,7 +241,7 @@ static void scan_mwctl_sensors(json_object *sensors_array)
/* Build command for popen - interface name is validated */
snprintf(cmd, sizeof(cmd), "/usr/sbin/mwctl %s stat 2>/dev/null", ifname);
fp = popen(cmd, "r");
fp = popen(cmd, "r"); // flawfinder: ignore
if (!fp)
continue;
@ -236,9 +250,13 @@ static void scan_mwctl_sensors(json_object *sensors_array)
if (strstr(output, "CurrentTemperature") != NULL) {
char *equals = strchr(output, '=');
if (equals) {
temp_value = atoi(equals + 1);
found_temp = 1;
break;
char *endptr;
long val = strtol(equals + 1, &endptr, 10);
if (endptr != equals + 1 && val >= INT_MIN && val <= INT_MAX) {
temp_value = (int)val;
found_temp = 1;
break;
}
}
}
}
@ -279,14 +297,16 @@ static void scan_wlctl_sensors(json_object *sensors_array)
/* Build command for popen - interface name is validated */
snprintf(cmd, sizeof(cmd), "/usr/sbin/wlctl -i %s phy_tempsense 2>/dev/null", ifname);
fp = popen(cmd, "r");
fp = popen(cmd, "r"); // flawfinder: ignore
if (!fp)
continue;
if (fgets(output, sizeof(output), fp) != NULL) {
/* Output is typically just a number or "Temperature: <value>" */
temp_value = atoi(output);
if (temp_value > 0) {
char *endptr;
long val = strtol(output, &endptr, 10);
if (endptr != output && val > 0 && val <= INT_MAX) {
temp_value = (int)val;
found_temp = 1;
}
}
@ -321,7 +341,7 @@ static void scan_thermal_zones(json_object *sensors_array)
if (strncmp(entry->d_name, "thermal_zone", 12) != 0)
continue;
char zone_dir[256];
char zone_dir[280]; /* THERMAL_PATH (19) + "/" + d_name (255) + null */
char temp_path[512];
char type_path[512];
char type[128] = {0};
@ -458,6 +478,48 @@ static int get_TemperatureSensor_value(char *refparam, struct dmctx *ctx, void *
return 0;
}
static int set_alarm_value_common(struct dmctx *ctx, void *data, char *value, int action, int trip_point_num, const char *hwmon_alarm_type)
{
json_object *obj = ((struct dm_data *)data)->json_object;
const char *sysfs_path;
int alarm_value_celsius;
switch (action) {
case VALUECHECK:
if (bbfdm_validate_string(ctx, value, -1, -1, NULL, NULL))
return FAULT_9007;
{
char *endptr;
long val = strtol(value, &endptr, 10);
if (endptr == value || val < -274 || val > INT_MAX)
return FAULT_9007;
alarm_value_celsius = (int)val;
}
break;
case VALUESET:
sysfs_path = dmjson_get_value(obj, 1, "sysfs_path");
if (!sysfs_path || sysfs_path[0] == '\0')
return FAULT_9002;
{
char *endptr;
long val = strtol(value, &endptr, 10);
if (endptr == value || val < INT_MIN || val > INT_MAX)
return FAULT_9002;
alarm_value_celsius = (int)val;
}
if (alarm_value_celsius == -274) {
/* -274 means disable/unconfigure - skip writing */
return 0;
}
if (write_alarm_value(sysfs_path, trip_point_num, hwmon_alarm_type, alarm_value_celsius * 1000) != 0)
return FAULT_9002;
break;
}
return 0;
}
static int get_TemperatureSensor_low_alarm_value(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
{
json_object *obj = ((struct dm_data *)data)->json_object;
@ -479,35 +541,8 @@ static int get_TemperatureSensor_low_alarm_value(char *refparam, struct dmctx *c
static int set_TemperatureSensor_low_alarm_value(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action)
{
json_object *obj = ((struct dm_data *)data)->json_object;
const char *sysfs_path;
int alarm_value_celsius;
switch (action) {
case VALUECHECK:
if (bbfdm_validate_string(ctx, value, -1, -1, NULL, NULL))
return FAULT_9007;
alarm_value_celsius = atoi(value);
if (alarm_value_celsius < -274)
return FAULT_9007;
break;
case VALUESET:
sysfs_path = dmjson_get_value(obj, 1, "sysfs_path");
if (!sysfs_path || sysfs_path[0] == '\0')
return FAULT_9002;
alarm_value_celsius = atoi(value);
if (alarm_value_celsius == -274) {
/* -274 means disable/unconfigure - skip writing */
return 0;
}
/* trip_point_1_temp for thermal zones, temp*_crit for hwmon */
if (write_alarm_value(sysfs_path, 1, "crit", alarm_value_celsius * 1000) != 0)
return FAULT_9002;
break;
}
return 0;
/* trip_point_1_temp for thermal zones, temp*_crit for hwmon */
return set_alarm_value_common(ctx, data, value, action, 1, "crit");
}
static int get_TemperatureSensor_high_alarm_value(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value)
@ -531,35 +566,8 @@ static int get_TemperatureSensor_high_alarm_value(char *refparam, struct dmctx *
static int set_TemperatureSensor_high_alarm_value(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action)
{
json_object *obj = ((struct dm_data *)data)->json_object;
const char *sysfs_path;
int alarm_value_celsius;
switch (action) {
case VALUECHECK:
if (bbfdm_validate_string(ctx, value, -1, -1, NULL, NULL))
return FAULT_9007;
alarm_value_celsius = atoi(value);
if (alarm_value_celsius < -274)
return FAULT_9007;
break;
case VALUESET:
sysfs_path = dmjson_get_value(obj, 1, "sysfs_path");
if (!sysfs_path || sysfs_path[0] == '\0')
return FAULT_9002;
alarm_value_celsius = atoi(value);
if (alarm_value_celsius == -274) {
/* -274 means disable/unconfigure - skip writing */
return 0;
}
/* trip_point_0_temp for thermal zones, temp*_max for hwmon */
if (write_alarm_value(sysfs_path, 0, "max", alarm_value_celsius * 1000) != 0)
return FAULT_9002;
break;
}
return 0;
/* trip_point_0_temp for thermal zones, temp*_max for hwmon */
return set_alarm_value_common(ctx, data, value, action, 0, "max");
}
/******************************************************************************************************************************