| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * sysfs.c sysfs ABI access functions for TMON program |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2013 Intel Corporation. All rights reserved. |
|---|
| 5 | 6 | * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or |
|---|
| 7 | | - * modify it under the terms of the GNU General Public License version |
|---|
| 8 | | - * 2 or later as published by the Free Software Foundation. |
|---|
| 9 | | - * |
|---|
| 10 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 11 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | | - * GNU General Public License for more details. |
|---|
| 14 | | - * |
|---|
| 15 | 7 | * Author: Jacob Pan <jacob.jun.pan@linux.intel.com> |
|---|
| 16 | | - * |
|---|
| 17 | 8 | */ |
|---|
| 18 | 9 | #include <unistd.h> |
|---|
| 19 | 10 | #include <stdio.h> |
|---|
| .. | .. |
|---|
| 22 | 13 | #include <stdint.h> |
|---|
| 23 | 14 | #include <dirent.h> |
|---|
| 24 | 15 | #include <libintl.h> |
|---|
| 16 | +#include <limits.h> |
|---|
| 25 | 17 | #include <ctype.h> |
|---|
| 26 | 18 | #include <time.h> |
|---|
| 27 | 19 | #include <syslog.h> |
|---|
| .. | .. |
|---|
| 42 | 34 | { |
|---|
| 43 | 35 | FILE *fd; |
|---|
| 44 | 36 | int ret = -1; |
|---|
| 45 | | - char filepath[256]; |
|---|
| 37 | + char filepath[PATH_MAX + 2]; /* NUL and '/' */ |
|---|
| 46 | 38 | |
|---|
| 47 | | - snprintf(filepath, 256, "%s/%s", path, filename); |
|---|
| 39 | + snprintf(filepath, sizeof(filepath), "%s/%s", path, filename); |
|---|
| 48 | 40 | |
|---|
| 49 | 41 | fd = fopen(filepath, "w"); |
|---|
| 50 | 42 | if (!fd) { |
|---|
| .. | .. |
|---|
| 66 | 58 | { |
|---|
| 67 | 59 | FILE *fd; |
|---|
| 68 | 60 | int ret = -1; |
|---|
| 69 | | - char filepath[256]; |
|---|
| 61 | + char filepath[PATH_MAX + 2]; /* NUL and '/' */ |
|---|
| 70 | 62 | |
|---|
| 71 | | - snprintf(filepath, 256, "%s/%s", path, filename); |
|---|
| 63 | + snprintf(filepath, sizeof(filepath), "%s/%s", path, filename); |
|---|
| 72 | 64 | |
|---|
| 73 | 65 | fd = fopen(filepath, "r"); |
|---|
| 74 | 66 | if (!fd) { |
|---|
| .. | .. |
|---|
| 85 | 77 | { |
|---|
| 86 | 78 | FILE *fd; |
|---|
| 87 | 79 | int ret = -1; |
|---|
| 88 | | - char filepath[256]; |
|---|
| 80 | + char filepath[PATH_MAX + 2]; /* NUL and '/' */ |
|---|
| 89 | 81 | |
|---|
| 90 | | - snprintf(filepath, 256, "%s/%s", path, filename); |
|---|
| 82 | + snprintf(filepath, sizeof(filepath), "%s/%s", path, filename); |
|---|
| 91 | 83 | |
|---|
| 92 | 84 | fd = fopen(filepath, "r"); |
|---|
| 93 | 85 | if (!fd) { |
|---|
| .. | .. |
|---|
| 208 | 200 | { |
|---|
| 209 | 201 | unsigned long trip_instance = 0; |
|---|
| 210 | 202 | char cdev_name_linked[256]; |
|---|
| 211 | | - char cdev_name[256]; |
|---|
| 212 | | - char cdev_trip_name[256]; |
|---|
| 203 | + char cdev_name[PATH_MAX]; |
|---|
| 204 | + char cdev_trip_name[PATH_MAX]; |
|---|
| 213 | 205 | int cdev_id; |
|---|
| 214 | 206 | |
|---|
| 215 | 207 | if (nl->d_type == DT_LNK) { |
|---|
| .. | .. |
|---|
| 222 | 214 | return -EINVAL; |
|---|
| 223 | 215 | } |
|---|
| 224 | 216 | /* find the link to real cooling device record binding */ |
|---|
| 225 | | - snprintf(cdev_name, 256, "%s/%s", tz_name, nl->d_name); |
|---|
| 217 | + snprintf(cdev_name, sizeof(cdev_name) - 2, "%s/%s", |
|---|
| 218 | + tz_name, nl->d_name); |
|---|
| 226 | 219 | memset(cdev_name_linked, 0, sizeof(cdev_name_linked)); |
|---|
| 227 | 220 | if (readlink(cdev_name, cdev_name_linked, |
|---|
| 228 | 221 | sizeof(cdev_name_linked) - 1) != -1) { |
|---|
| .. | .. |
|---|
| 235 | 228 | /* find the trip point in which the cdev is binded to |
|---|
| 236 | 229 | * in this tzone |
|---|
| 237 | 230 | */ |
|---|
| 238 | | - snprintf(cdev_trip_name, 256, "%s%s", nl->d_name, |
|---|
| 239 | | - "_trip_point"); |
|---|
| 231 | + snprintf(cdev_trip_name, sizeof(cdev_trip_name) - 1, |
|---|
| 232 | + "%s%s", nl->d_name, "_trip_point"); |
|---|
| 240 | 233 | sysfs_get_ulong(tz_name, cdev_trip_name, |
|---|
| 241 | 234 | &trip_instance); |
|---|
| 242 | 235 | /* validate trip point range, e.g. trip could return -1 |
|---|