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
diff --git a/cmds/screencap/screencap.cpp b/cmds/screencap/screencap.cpp
index ee9c464..2d23533 100644
--- a/cmds/screencap/screencap.cpp
+++ b/cmds/screencap/screencap.cpp
@@ -142,13 +142,22 @@
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) {