summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Dominik Laskowski <domlaskowski@google.com> 2023-09-28 17:10:47 -0400
committer Dominik Laskowski <domlaskowski@google.com> 2023-11-08 17:07:31 -0500
commitd8dc5b3f8af19d4f628843a89e158167b02f7c9d (patch)
tree6ec9a6f593f168cddb18f956df34d3d495ef927b
parentfa76ad2f83b019184fe2f57b2ef6be1d44a80adc (diff)
screencap: Fix parsing of display IDs
The underlying type of DisplayId is uint64_t, but screencap parsed the IDs as signed 64-bit, invoking undefined behavior for out-of-range IDs. In practice, out-of-range IDs were clamped, so SF failed to find them. Fixes: 302580952 Test: screencap works for virtual display on Felix Change-Id: I09a863d0c68dbb857b6f756b51159e5e3d853f5d
-rw-r--r--cmds/screencap/screencap.cpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/cmds/screencap/screencap.cpp b/cmds/screencap/screencap.cpp
index ee9c464219d9..2d235331a672 100644
--- a/cmds/screencap/screencap.cpp
+++ b/cmds/screencap/screencap.cpp
@@ -142,13 +142,22 @@ int main(int argc, char** argv)
case 'p':
png = true;
break;
- case 'd':
- displayIdOpt = DisplayId::fromValue(atoll(optarg));
+ case 'd': {
+ errno = 0;
+ char* end = nullptr;
+ const uint64_t id = strtoull(optarg, &end, 10);
+ if (!end || *end != '\0' || errno == ERANGE) {
+ fprintf(stderr, "Invalid display ID: Out of range [0, 2^64).\n");
+ return 1;
+ }
+
+ displayIdOpt = DisplayId::fromValue(id);
if (!displayIdOpt) {
- fprintf(stderr, "Invalid display ID: %s\n", optarg);
+ fprintf(stderr, "Invalid display ID: Incorrect encoding.\n");
return 1;
}
break;
+ }
case '?':
case 'h':
if (ids.size() == 1) {