aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorVitaly Buka <vitalybuka@google.com>2016-02-23 12:07:07 -0800
committerVitaly Buka <vitalybuka@google.com>2016-02-23 22:10:15 +0000
commit6da60ad25440c1e191e7e76c0e33d176ba98d5fb (patch)
tree17fc667e86c5ee7b108245f5f45b183448cc8ace /examples
parentc23ea22c3c3c9fbffc933da2c2896d4d1b920a72 (diff)
downloadlibweave-6da60ad25440c1e191e7e76c0e33d176ba98d5fb.tar.gz
Fix use-after-free detected by ASAN
Change-Id: Iba1c55f7f32b74f1d3b4ce31bec092527f83e5c8 Reviewed-on: https://weave-review.googlesource.com/2712 Reviewed-by: Alex Vakulenko <avakulenko@google.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/provider/wifi_manager.cc17
1 files changed, 9 insertions, 8 deletions
diff --git a/examples/provider/wifi_manager.cc b/examples/provider/wifi_manager.cc
index 7597e47..9337d23 100644
--- a/examples/provider/wifi_manager.cc
+++ b/examples/provider/wifi_manager.cc
@@ -46,20 +46,21 @@ int ForkCmdAndWait(const std::string& path,
return status;
}
+struct DirCloser {
+ void operator()(DIR* dir) { closedir(dir); }
+};
+
std::string FindWirelessInterface() {
std::string sysfs_net{"/sys/class/net"};
- DIR* net_dir = opendir(sysfs_net.c_str());
+ std::unique_ptr<DIR, DirCloser> net_dir{opendir(sysfs_net.c_str())};
+ CHECK(net_dir);
dirent* iface;
- while ((iface = readdir(net_dir))) {
+ while ((iface = readdir(net_dir.get()))) {
auto path = sysfs_net + "/" + iface->d_name + "/wireless";
- DIR* wireless_dir = opendir(path.c_str());
- if (wireless_dir != nullptr) {
- closedir(net_dir);
- closedir(wireless_dir);
+ std::unique_ptr<DIR, DirCloser> wireless_dir{opendir(path.c_str())};
+ if (wireless_dir)
return iface->d_name;
- }
}
- closedir(net_dir);
return "";
}