From: Davin McCall Date: Thu, 14 Jan 2016 18:19:41 +0000 (+0000) Subject: Remove some explicit memory deallocation in favor of using unique_ptr. X-Git-Tag: v0.01~25 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=1e494a9be7f6a16ed36649a300bed2eee51123b3;p=oweals%2Fdinit.git Remove some explicit memory deallocation in favor of using unique_ptr. --- diff --git a/src/dinitctl.cc b/src/dinitctl.cc index 8a7f111..0928bb8 100644 --- a/src/dinitctl.cc +++ b/src/dinitctl.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -241,14 +242,19 @@ int main(int argc, char **argv) // Build buffer; uint16_t sname_len = strlen(service_name); int bufsize = 3 + sname_len; - char * buf = new char[bufsize]; + int r; - buf[0] = DINIT_CP_LOADSERVICE; - memcpy(buf + 1, &sname_len, 2); - memcpy(buf + 3, service_name, sname_len); + { + unique_ptr ubuf(new char[bufsize]); + auto *buf = ubuf.get(); + + buf[0] = DINIT_CP_LOADSERVICE; + memcpy(buf + 1, &sname_len, 2); + memcpy(buf + 3, service_name, sname_len); + + r = write_all(socknum, buf, bufsize); + } - int r = write_all(socknum, buf, bufsize); - delete [] buf; if (r == -1) { perror("write"); return 1; @@ -288,12 +294,15 @@ int main(int argc, char **argv) // start/stop also sets or clears the "explicitly started" flag on the service. //if (target_state != wanted_state) { { - buf = new char[2 + sizeof(handle)]; - buf[0] = command; - buf[1] = do_pin ? 1 : 0; - memcpy(buf + 2, &handle, sizeof(handle)); - r = write_all(socknum, buf, 2 + sizeof(handle)); - delete [] buf; + { + auto buf = new char[2 + sizeof(handle)]; + unique_ptr ubuf(buf); + + buf[0] = command; + buf[1] = do_pin ? 1 : 0; + memcpy(buf + 2, &handle, sizeof(handle)); + r = write_all(socknum, buf, 2 + sizeof(handle)); + } if (r == -1) { perror("write"); @@ -413,14 +422,19 @@ static int unpinService(int socknum, const char *service_name) // Build buffer; uint16_t sname_len = strlen(service_name); int bufsize = 3 + sname_len; - char * buf = new char[bufsize]; + int r; - buf[0] = DINIT_CP_LOADSERVICE; - memcpy(buf + 1, &sname_len, 2); - memcpy(buf + 3, service_name, sname_len); + { + char * buf = new char[bufsize]; + unique_ptr ubuf(buf); + + buf[0] = DINIT_CP_LOADSERVICE; + memcpy(buf + 1, &sname_len, 2); + memcpy(buf + 3, service_name, sname_len); + + r = write_all(socknum, buf, bufsize); + } - int r = write_all(socknum, buf, bufsize); - delete [] buf; if (r == -1) { perror("write"); return 1; @@ -454,11 +468,13 @@ static int unpinService(int socknum, const char *service_name) // Issue UNPIN command. { - buf = new char[1 + sizeof(handle)]; - buf[0] = DINIT_CP_UNPINSERVICE; - memcpy(buf + 1, &handle, sizeof(handle)); - r = write_all(socknum, buf, 2 + sizeof(handle)); - delete [] buf; + { + char *buf = new char[1 + sizeof(handle)]; + unique_ptr ubuf(buf); + buf[0] = DINIT_CP_UNPINSERVICE; + memcpy(buf + 1, &handle, sizeof(handle)); + r = write_all(socknum, buf, 2 + sizeof(handle)); + } if (r == -1) { perror("write");