summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTvrtko Ursulin <tvrtko.ursulin@intel.com>2022-05-27 11:50:41 +0100
committerPetri Latvala <petri.latvala@intel.com>2022-05-30 16:19:50 +0300
commitae1864064454df483e3298e7b030fa3c7624fc18 (patch)
treea20307e4f6885a76085d4a69069b73083481faf4 /lib
parent1351a97b3dea0a7e984edb4f87ef1b19b311083d (diff)
lib/drm_fdinfo: Ensure buffer is null terminated
Ensure buffer is null terminated at the point where the read ended and not at the end of the whole buffer. Otherwise string parsing can stray into un-initialised memory. Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com> Reviewed-by: Petri Latvala <petri.latvala@intel.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/igt_drm_fdinfo.c8
-rw-r--r--lib/igt_drm_fdinfo.h4
2 files changed, 6 insertions, 6 deletions
diff --git a/lib/igt_drm_fdinfo.c b/lib/igt_drm_fdinfo.c
index b422f67a..250d9e89 100644
--- a/lib/igt_drm_fdinfo.c
+++ b/lib/igt_drm_fdinfo.c
@@ -44,12 +44,12 @@ static size_t read_fdinfo(char *buf, const size_t sz, int at, const char *name)
if (fd < 0)
return 0;
- buf[sz - 1] = 0;
- count = read(fd, buf, sz);
- buf[sz - 1] = 0;
+ count = read(fd, buf, sz - 1);
+ if (count > 0)
+ buf[count - 1] = 0;
close(fd);
- return count;
+ return count > 0 ? count : 0;
}
static int parse_engine(char *line, struct drm_client_fdinfo *info,
diff --git a/lib/igt_drm_fdinfo.h b/lib/igt_drm_fdinfo.h
index 5db63e28..87594716 100644
--- a/lib/igt_drm_fdinfo.h
+++ b/lib/igt_drm_fdinfo.h
@@ -46,7 +46,7 @@ struct drm_client_fdinfo {
* igt_parse_drm_fdinfo: Parses the drm fdinfo file
*
* @drm_fd: DRM file descriptor
- * @info: Structure to populate with read data
+ * @info: Structure to populate with read data. Must be zeroed.
*
* Returns the number of valid drm fdinfo keys found or zero if not all
* mandatory keys were present or no engines found.
@@ -58,7 +58,7 @@ unsigned int igt_parse_drm_fdinfo(int drm_fd, struct drm_client_fdinfo *info);
*
* @dir: File descriptor pointing to /proc/pid/fdinfo directory
* @fd: String representation of the file descriptor number to parse.
- * @info: Structure to populate with read data
+ * @info: Structure to populate with read data. Must be zeroed.
*
* Returns the number of valid drm fdinfo keys found or zero if not all
* mandatory keys were present or no engines found.