hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/tools/thermal/tmon/sysfs.c
....@@ -1,19 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * sysfs.c sysfs ABI access functions for TMON program
34 *
45 * Copyright (C) 2013 Intel Corporation. All rights reserved.
56 *
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
- *
157 * Author: Jacob Pan <jacob.jun.pan@linux.intel.com>
16
- *
178 */
189 #include <unistd.h>
1910 #include <stdio.h>
....@@ -22,6 +13,7 @@
2213 #include <stdint.h>
2314 #include <dirent.h>
2415 #include <libintl.h>
16
+#include <limits.h>
2517 #include <ctype.h>
2618 #include <time.h>
2719 #include <syslog.h>
....@@ -42,9 +34,9 @@
4234 {
4335 FILE *fd;
4436 int ret = -1;
45
- char filepath[256];
37
+ char filepath[PATH_MAX + 2]; /* NUL and '/' */
4638
47
- snprintf(filepath, 256, "%s/%s", path, filename);
39
+ snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
4840
4941 fd = fopen(filepath, "w");
5042 if (!fd) {
....@@ -66,9 +58,9 @@
6658 {
6759 FILE *fd;
6860 int ret = -1;
69
- char filepath[256];
61
+ char filepath[PATH_MAX + 2]; /* NUL and '/' */
7062
71
- snprintf(filepath, 256, "%s/%s", path, filename);
63
+ snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
7264
7365 fd = fopen(filepath, "r");
7466 if (!fd) {
....@@ -85,9 +77,9 @@
8577 {
8678 FILE *fd;
8779 int ret = -1;
88
- char filepath[256];
80
+ char filepath[PATH_MAX + 2]; /* NUL and '/' */
8981
90
- snprintf(filepath, 256, "%s/%s", path, filename);
82
+ snprintf(filepath, sizeof(filepath), "%s/%s", path, filename);
9183
9284 fd = fopen(filepath, "r");
9385 if (!fd) {
....@@ -208,8 +200,8 @@
208200 {
209201 unsigned long trip_instance = 0;
210202 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];
213205 int cdev_id;
214206
215207 if (nl->d_type == DT_LNK) {
....@@ -222,7 +214,8 @@
222214 return -EINVAL;
223215 }
224216 /* 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);
226219 memset(cdev_name_linked, 0, sizeof(cdev_name_linked));
227220 if (readlink(cdev_name, cdev_name_linked,
228221 sizeof(cdev_name_linked) - 1) != -1) {
....@@ -235,8 +228,8 @@
235228 /* find the trip point in which the cdev is binded to
236229 * in this tzone
237230 */
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");
240233 sysfs_get_ulong(tz_name, cdev_trip_name,
241234 &trip_instance);
242235 /* validate trip point range, e.g. trip could return -1