Browse Source

Change int to unsigned as appropriate in cpbuffer

Davin McCall 1 year ago
parent
commit
49bc0db8b2
5 changed files with 38 additions and 37 deletions
  1. 2 2
      src/dinit-log.cc
  2. 12 11
      src/dinitctl.cc
  3. 1 1
      src/includes/control.h
  4. 22 22
      src/includes/cpbuffer.h
  5. 1 1
      src/shutdown.cc

+ 2 - 2
src/dinit-log.cc

@@ -208,7 +208,7 @@ rearm buffered_log_stream::fd_event(eventloop_t &loop, int fd, int flags) noexce
         struct iovec logiov[2];
         
         char *ptr = log_buffer.get_ptr(0);
-        int len = log_buffer.get_contiguous_length(ptr);
+        unsigned len = log_buffer.get_contiguous_length(ptr);
         char *creptr = ptr + len;  // contiguous region end
         char *eptr = std::find(ptr, creptr, '\n');
         
@@ -225,7 +225,7 @@ rearm buffered_log_stream::fd_event(eventloop_t &loop, int fd, int flags) noexce
         int iovs_to_write = 1;
         
         // Do we need the second span?
-        if (! will_complete && len != log_buffer.get_length()) {
+        if (!will_complete && len != log_buffer.get_length()) {
             ptr = log_buffer.get_buf_base();
             creptr = ptr + log_buffer.get_length() - len;
             eptr = std::find(ptr, creptr, '\n');

+ 12 - 11
src/dinitctl.cc

@@ -928,7 +928,7 @@ static int list_services(int socknum, cpbuffer_t &rbuffer)
     while (rbuffer[0] == DINIT_RP_SVCINFO) {
         int hdrsize = 8 + std::max(sizeof(int), sizeof(pid_t));
         fill_buffer_to(rbuffer, socknum, hdrsize);
-        int nameLen = rbuffer[1];
+        unsigned name_len = (unsigned char)rbuffer[1];
         service_state_t current = static_cast<service_state_t>(rbuffer[2]);
         service_state_t target = static_cast<service_state_t>(rbuffer[3]);
 
@@ -949,13 +949,13 @@ static int list_services(int socknum, cpbuffer_t &rbuffer)
         	rbuffer.extract((char *)&exit_status, 8, sizeof(exit_status));
         }
 
-        fill_buffer_to(rbuffer, socknum, nameLen + hdrsize);
+        fill_buffer_to(rbuffer, socknum, name_len + hdrsize);
 
         char *name_ptr = rbuffer.get_ptr(hdrsize);
-        int clength = std::min(rbuffer.get_contiguous_length(name_ptr), nameLen);
+        unsigned clength = std::min(rbuffer.get_contiguous_length(name_ptr), name_len);
 
         string name = string(name_ptr, clength);
-        name.append(rbuffer.get_buf_base(), nameLen - clength);
+        name.append(rbuffer.get_buf_base(), name_len - clength);
 
         cout << "[";
 
@@ -1023,7 +1023,7 @@ static int list_services(int socknum, cpbuffer_t &rbuffer)
 
         cout << endl;
 
-        rbuffer.consume(hdrsize + nameLen);
+        rbuffer.consume(hdrsize + name_len);
         wait_for_reply(rbuffer, socknum);
     }
 
@@ -1490,10 +1490,11 @@ static int do_setenv(int socknum, cpbuffer_t &rbuffer, std::vector<const char *>
         // protocol message and size space
         buf.push_back(DINIT_CP_SETENV);
         buf.append(2, 0);
+        const unsigned hdr_len = 3;
         // either full var or name
         auto elen = strlen(envp);
         buf.append(envp, elen);
-        // = not found, get value from environment
+        // if '=' not found, get value from environment
         if (!memchr(envp, '=', elen)) {
             buf.push_back('=');
             auto *envv = getenv(envp);
@@ -1501,18 +1502,18 @@ static int do_setenv(int socknum, cpbuffer_t &rbuffer, std::vector<const char *>
                 buf.append(envv);
             }
         }
-        uint16_t bufs = buf.size() - 3;
+        uint16_t bufs = buf.size() - hdr_len;
         // sanitize length early on
-        if (bufs > (1024 - 3)) {
-            auto eq = buf.find('=', 3);
-            auto name = buf.substr(3, eq - 3);
+        if (buf.size() > cpbuffer_t::get_size()) {
+            auto eq = buf.find('=', hdr_len);
+            auto name = buf.substr(hdr_len, eq - hdr_len);
             cerr << "dinitctl: environment variable '" << name << "' too long." << endl;
             return 1;
         }
         // set size in protocol message
         memcpy(&buf[1], &bufs, 2);
         // send
-        write_all_x(socknum, buf.data(), bufs + 3);
+        write_all_x(socknum, buf.data(), buf.size());
         wait_for_reply(rbuffer, socknum);
         if (rbuffer[0] == DINIT_RP_BADREQ) {
             cerr << "dinitctl: failed to export environment." << endl;

+ 1 - 1
src/includes/control.h

@@ -98,7 +98,7 @@ class control_conn_t : private service_listener
 
     // The packet length before we need to re-check if the packet is complete.
     // process_packet() will not be called until the packet reaches this size.
-    int chklen;
+    unsigned chklen;
     
     // Receive buffer
     cpbuffer<1024> rbuf;

+ 22 - 22
src/includes/cpbuffer.h

@@ -8,19 +8,19 @@
 #include "baseproc-sys.h"
 
 // control protocol buffer, a circular buffer with fixed capacity.
-template <int SIZE> class cpbuffer
+template <unsigned SIZE> class cpbuffer
 {
     char buf[SIZE];
-    int cur_idx = 0;
-    int length = 0;  // number of elements in the buffer
+    unsigned cur_idx = 0;
+    unsigned length = 0;  // number of elements in the buffer
     
     public:
-    static constexpr int get_size()
+    static constexpr unsigned get_size()
     {
         return SIZE;
     }
 
-    int get_length() noexcept
+    unsigned get_length() noexcept
     {
         return length;
     }
@@ -32,7 +32,7 @@ template <int SIZE> class cpbuffer
     
     char * get_ptr(int index)
     {
-        int pos = cur_idx + index;
+        unsigned pos = cur_idx + index;
         if (pos >= SIZE) pos -= SIZE;
     
         return &buf[pos];
@@ -43,9 +43,9 @@ template <int SIZE> class cpbuffer
         return buf;
     }
     
-    int get_contiguous_length(char *ptr)
+    unsigned get_contiguous_length(char *ptr)
     {
-        int eidx = cur_idx + length;
+        unsigned eidx = cur_idx + length;
         if (eidx >= SIZE) eidx -= SIZE;
         
         if (buf + eidx > ptr) {
@@ -59,9 +59,9 @@ template <int SIZE> class cpbuffer
     // Fill by reading from the given fd, return positive if some was read or -1 on error.
     int fill(int fd) noexcept
     {
-        int pos = cur_idx + length;
+        unsigned pos = cur_idx + length;
         if (pos >= SIZE) pos -= SIZE;
-        int max_count = std::min(SIZE - pos, SIZE - length);
+        unsigned max_count = std::min(SIZE - pos, SIZE - length);
         ssize_t r = bp_sys::read(fd, buf + pos, max_count);
         if (r >= 0) {
             length += r;
@@ -71,11 +71,11 @@ template <int SIZE> class cpbuffer
     
     // Fill by reading up to the specified amount of bytes from the given fd,
     // Return is the number of bytes read, 0 on end-of-file or -1 on error.
-    int fill(int fd, int limit) noexcept
+    int fill(int fd, unsigned limit) noexcept
     {
-        int pos = cur_idx + length;
+        unsigned pos = cur_idx + length;
         if (pos >= SIZE) pos -= SIZE;
-        int max_count = std::min(SIZE - pos, SIZE - length);
+        unsigned max_count = std::min(SIZE - pos, SIZE - length);
         max_count = std::min(max_count, limit);
         ssize_t r = bp_sys::read(fd, buf + pos, max_count);
         if (r >= 0) {
@@ -86,7 +86,7 @@ template <int SIZE> class cpbuffer
 
     // fill by reading from the given fd, until at least the specified number of bytes are in
     // the buffer. Return 0 if end-of-file reached before fill complete, or -1 on error.
-    int fill_to(int fd, int rlength) noexcept
+    int fill_to(int fd, unsigned rlength) noexcept
     {
         while (length < rlength) {
             int r = fill(fd);
@@ -96,20 +96,20 @@ template <int SIZE> class cpbuffer
     }
     
     // Trim the buffer to the specified length (must be less than current length)
-    void trim_to(int new_length)
+    void trim_to(unsigned new_length)
     {
         length = new_length;
     }
     
     char operator[](int idx) noexcept
     {
-        int dest_idx = cur_idx + idx;
+        unsigned dest_idx = cur_idx + idx;
         if (dest_idx >= SIZE) dest_idx -= SIZE;
         return buf[dest_idx];
     }
     
     // Remove the given number of bytes from the start of the buffer.
-    void consume(int amount) noexcept
+    void consume(unsigned amount) noexcept
     {
         cur_idx += amount;
         if (cur_idx >= SIZE) cur_idx -= SIZE;
@@ -117,7 +117,7 @@ template <int SIZE> class cpbuffer
     }
     
     // Extract bytes from the buffer. The bytes remain in the buffer.
-    void extract(void *dest, int index, int length) noexcept
+    void extract(void *dest, unsigned index, unsigned length) noexcept
     {
         index += cur_idx;
         if (index >= SIZE) index -= SIZE;
@@ -135,7 +135,7 @@ template <int SIZE> class cpbuffer
     
     // Extract string of given length from given index
     // Throws:  std::bad_alloc on allocation failure
-    std::string extract_string(int index, int length)
+    std::string extract_string(unsigned index, unsigned length)
     {
         index += cur_idx;
         if (index >= SIZE) index -= SIZE;
@@ -151,14 +151,14 @@ template <int SIZE> class cpbuffer
     
     // Append characters to the buffer. Caller must make certain there
     // is enough space to contain the characters first.
-    void append(const char * s, int len) noexcept
+    void append(const char * s, unsigned len) noexcept
     {
-        int index = cur_idx + length;
+        unsigned index = cur_idx + length;
         if (index >= SIZE) index -= SIZE;
 
         length += len; // (before we destroy len)
         
-        int max = SIZE - index;
+        unsigned max = SIZE - index;
         std::memcpy(buf + index, s, std::min(max, len));
         if (len > max) {
             // Wrapped around buffer: copy the rest

+ 1 - 1
src/shutdown.cc

@@ -111,7 +111,7 @@ class subproc_buffer : private cpbuffer<subproc_bufsize>
     void append(const char *msg)
     {
         out_watch->set_enabled(loop, true);
-        int len = strlen(msg);
+        unsigned len = strlen(msg);
         if (subproc_bufsize - get_length() >= len) {
             base::append(msg, len);
         }