diff --git a/libtransmission/file-posix.cc b/libtransmission/file-posix.cc index a108f0731..4b34d7d7d 100644 --- a/libtransmission/file-posix.cc +++ b/libtransmission/file-posix.cc @@ -185,7 +185,7 @@ static bool create_path_require_dir(char const* path, tr_error** error) static bool create_path(char const* path_in, int permissions, tr_error** error) { /* make a temporary copy of path */ - char* path = tr_strdup(path_in); + char* const path = tr_strdup(path_in); /* walk past the root */ char* p = path; @@ -202,7 +202,7 @@ static bool create_path(char const* path_in, int permissions, tr_error** error) --path_end; } - char* pp; + char* pp = nullptr; bool ret = false; tr_error* my_error = nullptr; @@ -297,7 +297,7 @@ bool tr_sys_path_get_info(char const* path, int flags, tr_sys_path_info* info, t TR_ASSERT(path != nullptr); TR_ASSERT(info != nullptr); - bool ret; + bool ret = false; struct stat sb; if ((flags & TR_SYS_PATH_NO_FOLLOW) == 0) @@ -384,11 +384,8 @@ char* tr_sys_path_basename(char const* path, tr_error** error) { TR_ASSERT(path != nullptr); - char* ret = nullptr; - char* tmp; - - tmp = tr_strdup(path); - ret = basename(tmp); + char* const tmp = tr_strdup(path); + char* ret = basename(tmp); if (ret != nullptr) { @@ -408,7 +405,7 @@ char* tr_sys_path_dirname(char const* path, tr_error** error) { TR_ASSERT(path != nullptr); - char* tmp = tr_strdup(path); + char* const tmp = tr_strdup(path); char* ret = dirname(tmp); if (ret != nullptr) @@ -430,7 +427,7 @@ bool tr_sys_path_rename(char const* src_path, char const* dst_path, tr_error** e TR_ASSERT(src_path != nullptr); TR_ASSERT(dst_path != nullptr); - bool ret = rename(src_path, dst_path) != -1; + bool const ret = rename(src_path, dst_path) != -1; if (!ret) { @@ -563,7 +560,7 @@ bool tr_sys_path_remove(char const* path, tr_error** error) { TR_ASSERT(path != nullptr); - bool ret = remove(path) != -1; + bool const ret = remove(path) != -1; if (!ret) { @@ -637,7 +634,7 @@ tr_sys_file_t tr_sys_file_open(char const* path, int flags, int permissions, tr_ } } - tr_sys_file_t ret = open(path, native_flags, permissions); + tr_sys_file_t const ret = open(path, native_flags, permissions); if (ret != TR_BAD_SYS_FILE) { @@ -658,7 +655,7 @@ tr_sys_file_t tr_sys_file_open_temp(char* path_template, tr_error** error) { TR_ASSERT(path_template != nullptr); - tr_sys_file_t ret = mkstemp(path_template); + tr_sys_file_t const ret = mkstemp(path_template); if (ret == TR_BAD_SYS_FILE) { @@ -674,7 +671,7 @@ bool tr_sys_file_close(tr_sys_file_t handle, tr_error** error) { TR_ASSERT(handle != TR_BAD_SYS_FILE); - bool ret = close(handle) != -1; + bool const ret = close(handle) != -1; if (!ret) { @@ -690,7 +687,7 @@ bool tr_sys_file_get_info(tr_sys_file_t handle, tr_sys_path_info* info, tr_error TR_ASSERT(info != nullptr); struct stat sb; - bool ret = fstat(handle, &sb) != -1; + bool const ret = fstat(handle, &sb) != -1; if (ret) { @@ -714,12 +711,10 @@ bool tr_sys_file_seek(tr_sys_file_t handle, int64_t offset, tr_seek_origin_t ori TR_ASSERT(origin == TR_SEEK_SET || origin == TR_SEEK_CUR || origin == TR_SEEK_END); bool ret = false; - off_t my_new_offset; + off_t const my_new_offset = lseek(handle, offset, origin); static_assert(sizeof(*new_offset) >= sizeof(my_new_offset), ""); - my_new_offset = lseek(handle, offset, origin); - if (my_new_offset != -1) { if (new_offset != nullptr) @@ -743,12 +738,10 @@ bool tr_sys_file_read(tr_sys_file_t handle, void* buffer, uint64_t size, uint64_ TR_ASSERT(buffer != nullptr || size == 0); bool ret = false; - ssize_t my_bytes_read; + ssize_t const my_bytes_read = read(handle, buffer, size); static_assert(sizeof(*bytes_read) >= sizeof(my_bytes_read), ""); - my_bytes_read = read(handle, buffer, size); - if (my_bytes_read != -1) { if (bytes_read != nullptr) @@ -780,27 +773,19 @@ bool tr_sys_file_read_at( TR_ASSERT(offset < UINT64_MAX / 2); bool ret = false; - ssize_t my_bytes_read; - - static_assert(sizeof(*bytes_read) >= sizeof(my_bytes_read), ""); #ifdef HAVE_PREAD - my_bytes_read = pread(handle, buffer, size, offset); + ssize_t const my_bytes_read = pread(handle, buffer, size, offset); #else - if (lseek(handle, offset, SEEK_SET) != -1) - { - my_bytes_read = read(handle, buffer, size); - } - else - { - my_bytes_read = -1; - } + ssize_t const my_bytes_read = lseek(handle, offset, SEEK_SET) == -1 ? -1 : read(handle, buffer, size); #endif + static_assert(sizeof(*bytes_read) >= sizeof(my_bytes_read), ""); + if (my_bytes_read != -1) { if (bytes_read != nullptr) @@ -824,12 +809,10 @@ bool tr_sys_file_write(tr_sys_file_t handle, void const* buffer, uint64_t size, TR_ASSERT(buffer != nullptr || size == 0); bool ret = false; - ssize_t my_bytes_written; + ssize_t const my_bytes_written = write(handle, buffer, size); static_assert(sizeof(*bytes_written) >= sizeof(my_bytes_written), ""); - my_bytes_written = write(handle, buffer, size); - if (my_bytes_written != -1) { if (bytes_written != nullptr) @@ -861,27 +844,19 @@ bool tr_sys_file_write_at( TR_ASSERT(offset < UINT64_MAX / 2); bool ret = false; - ssize_t my_bytes_written; - - static_assert(sizeof(*bytes_written) >= sizeof(my_bytes_written), ""); #ifdef HAVE_PWRITE - my_bytes_written = pwrite(handle, buffer, size, offset); + ssize_t const my_bytes_written = pwrite(handle, buffer, size, offset); #else - if (lseek(handle, offset, SEEK_SET) != -1) - { - my_bytes_written = write(handle, buffer, size); - } - else - { - my_bytes_written = -1; - } + ssize_t const my_bytes_written = lseek(handle, offset, SEEK_SET) == -1 ? -1 : write(handle, buffer, size); #endif + static_assert(sizeof(*bytes_written) >= sizeof(my_bytes_written), ""); + if (my_bytes_written != -1) { if (bytes_written != nullptr) @@ -1137,7 +1112,7 @@ bool tr_sys_file_lock([[maybe_unused]] tr_sys_file_t handle, [[maybe_unused]] in TR_ASSERT( !!(operation & TR_SYS_FILE_LOCK_SH) + !!(operation & TR_SYS_FILE_LOCK_EX) + !!(operation & TR_SYS_FILE_LOCK_UN) == 1); - bool ret; + bool ret = false; #if defined(F_OFD_SETLK) @@ -1216,9 +1191,7 @@ bool tr_sys_file_lock([[maybe_unused]] tr_sys_file_t handle, [[maybe_unused]] in char* tr_sys_dir_get_current(tr_error** error) { - char* ret; - - ret = getcwd(nullptr, 0); + char* ret = getcwd(nullptr, 0); if (ret == nullptr && (errno == EINVAL || errno == ERANGE)) { @@ -1258,7 +1231,7 @@ bool tr_sys_dir_create(char const* path, int flags, int permissions, tr_error** { TR_ASSERT(path != nullptr); - bool ret; + bool ret = false; tr_error* my_error = nullptr; if ((flags & TR_SYS_DIR_CREATE_PARENTS) != 0) @@ -1308,15 +1281,13 @@ bool tr_sys_dir_create_temp(char* path_template, tr_error** error) { TR_ASSERT(path_template != nullptr); - bool ret; - #ifdef HAVE_MKDTEMP - ret = mkdtemp(path_template) != nullptr; + bool const ret = mkdtemp(path_template) != nullptr; #else - ret = mktemp(path_template) != nullptr && mkdir(path_template, 0700) != -1; + bool const ret = mktemp(path_template) != nullptr && mkdir(path_template, 0700) != -1; #endif diff --git a/libtransmission/handshake.cc b/libtransmission/handshake.cc index 52679fd33..b0ea5b9e1 100644 --- a/libtransmission/handshake.cc +++ b/libtransmission/handshake.cc @@ -287,16 +287,16 @@ static handshake_parse_err_t parseHandshake(tr_handshake* handshake, struct evbu /* 1 A->B: Diffie Hellman Ya, PadA */ static void sendYa(tr_handshake* handshake) { - int len; - char outbuf[KEY_LEN + PadA_MAXLEN]; - char* walk = outbuf; - /* add our public key (Ya) */ + + int len = 0; uint8_t const* const public_key = tr_cryptoGetMyPublicKey(handshake->crypto, &len); TR_ASSERT(len == KEY_LEN); TR_ASSERT(public_key != nullptr); - memcpy(walk, public_key, len); - walk += len; + + char outbuf[KEY_LEN + PadA_MAXLEN]; + char* walk = outbuf; + walk = std::copy_n(public_key, len, walk); /* add some bullshit padding */ len = tr_rand_int(PadA_MAXLEN); @@ -512,8 +512,6 @@ static ReadState readVC(tr_handshake* handshake, struct evbuffer* inbuf) static ReadState readCryptoSelect(tr_handshake* handshake, struct evbuffer* inbuf) { - uint16_t pad_d_len; - uint32_t crypto_select; static size_t const needlen = sizeof(uint32_t) + sizeof(uint16_t); if (evbuffer_get_length(inbuf) < needlen) @@ -521,6 +519,7 @@ static ReadState readCryptoSelect(tr_handshake* handshake, struct evbuffer* inbu return READ_LATER; } + uint32_t crypto_select = 0; tr_peerIoReadUint32(handshake->io, inbuf, &crypto_select); handshake->crypto_select = crypto_select; dbgmsg(handshake, "crypto select is %d", (int)crypto_select); @@ -531,6 +530,7 @@ static ReadState readCryptoSelect(tr_handshake* handshake, struct evbuffer* inbu return tr_handshakeDone(handshake, false); } + uint16_t pad_d_len = 0; tr_peerIoReadUint16(handshake->io, inbuf, &pad_d_len); dbgmsg(handshake, "pad_d_len is %d", (int)pad_d_len); @@ -573,11 +573,6 @@ static ReadState readPadD(tr_handshake* handshake, struct evbuffer* inbuf) static ReadState readHandshake(tr_handshake* handshake, struct evbuffer* inbuf) { - uint8_t pstrlen; - uint8_t pstr[20]; - uint8_t reserved[HANDSHAKE_FLAGS_LEN]; - uint8_t hash[SHA_DIGEST_LENGTH]; - dbgmsg(handshake, "payload: need %d, got %zu", INCOMING_HANDSHAKE_LEN, evbuffer_get_length(inbuf)); if (evbuffer_get_length(inbuf) < INCOMING_HANDSHAKE_LEN) @@ -587,7 +582,7 @@ static ReadState readHandshake(tr_handshake* handshake, struct evbuffer* inbuf) handshake->haveReadAnythingFromPeer = true; - pstrlen = evbuffer_pullup(inbuf, 1)[0]; /* peek, don't read. We may be handing inbuf to AWAITING_YA */ + uint8_t pstrlen = evbuffer_pullup(inbuf, 1)[0]; /* peek, don't read. We may be handing inbuf to AWAITING_YA */ if (pstrlen == 19) /* unencrypted */ { @@ -623,6 +618,7 @@ static ReadState readHandshake(tr_handshake* handshake, struct evbuffer* inbuf) /* pstr (BitTorrent) */ TR_ASSERT(pstrlen == 19); + uint8_t pstr[20]; tr_peerIoReadBytes(handshake->io, inbuf, pstr, pstrlen); pstr[pstrlen] = '\0'; @@ -632,6 +628,7 @@ static ReadState readHandshake(tr_handshake* handshake, struct evbuffer* inbuf) } /* reserved bytes */ + uint8_t reserved[HANDSHAKE_FLAGS_LEN]; tr_peerIoReadBytes(handshake->io, inbuf, reserved, sizeof(reserved)); /** @@ -643,6 +640,7 @@ static ReadState readHandshake(tr_handshake* handshake, struct evbuffer* inbuf) tr_peerIoEnableFEXT(handshake->io, HANDSHAKE_HAS_FASTEXT(reserved)); /* torrent hash */ + uint8_t hash[SHA_DIGEST_LENGTH]; tr_peerIoReadBytes(handshake->io, inbuf, hash, sizeof(hash)); if (tr_peerIoIsIncoming(handshake->io)) @@ -712,11 +710,6 @@ static ReadState readPeerId(tr_handshake* handshake, struct evbuffer* inbuf) static ReadState readYa(tr_handshake* handshake, struct evbuffer* inbuf) { - uint8_t ya[KEY_LEN]; - uint8_t* walk; - uint8_t outbuf[KEY_LEN + PadB_MAXLEN]; - int len; - dbgmsg(handshake, "in readYa... need %d, have %zu", KEY_LEN, evbuffer_get_length(inbuf)); if (evbuffer_get_length(inbuf) < KEY_LEN) @@ -725,6 +718,7 @@ static ReadState readYa(tr_handshake* handshake, struct evbuffer* inbuf) } /* read the incoming peer's public key */ + uint8_t ya[KEY_LEN]; evbuffer_remove(inbuf, ya, KEY_LEN); if (!tr_cryptoComputeSecret(handshake->crypto, ya)) @@ -736,10 +730,11 @@ static ReadState readYa(tr_handshake* handshake, struct evbuffer* inbuf) /* send our public key to the peer */ dbgmsg(handshake, "sending B->A: Diffie Hellman Yb, PadB"); - walk = outbuf; + uint8_t outbuf[KEY_LEN + PadB_MAXLEN]; + uint8_t* walk = outbuf; + int len = 0; uint8_t const* const myKey = tr_cryptoGetMyPublicKey(handshake->crypto, &len); - memcpy(walk, myKey, len); - walk += len; + walk = std::copy_n(myKey, len, walk); len = tr_rand_int(PadB_MAXLEN); tr_rand_buffer(walk, len); walk += len; @@ -844,7 +839,7 @@ static ReadState readCryptoProvide(tr_handshake* handshake, struct evbuffer* inb static ReadState readPadC(tr_handshake* handshake, struct evbuffer* inbuf) { - uint16_t ia_len; + uint16_t ia_len = 0; size_t const needlen = handshake->pad_c_len + sizeof(uint16_t); if (evbuffer_get_length(inbuf) < needlen) @@ -981,7 +976,6 @@ static ReadState canRead(tr_peerIo* io, void* vhandshake, size_t* piece) { TR_ASSERT(tr_isPeerIo(io)); - ReadState ret; auto* handshake = static_cast(vhandshake); struct evbuffer* inbuf = tr_peerIoGetReadBuffer(io); @@ -992,6 +986,7 @@ static ReadState canRead(tr_peerIo* io, void* vhandshake, size_t* piece) dbgmsg(handshake, "handling canRead; state is [%s]", getStateName(handshake->state)); + ReadState ret = READ_NOW; while (readyForMore) { switch (handshake->state) diff --git a/libtransmission/platform-quota.cc b/libtransmission/platform-quota.cc index 07a15d005..041de0278 100644 --- a/libtransmission/platform-quota.cc +++ b/libtransmission/platform-quota.cc @@ -80,18 +80,15 @@ static char const* getdev(char const* path) { #ifdef HAVE_GETMNTENT - FILE* fp; - #ifdef __sun - struct mnttab mnt; - fp = fopen(_PATH_MOUNTED, "r"); - + FILE* const fp = fopen(_PATH_MOUNTED, "r"); if (fp == nullptr) { return nullptr; } + struct mnttab mnt; while (getmntent(fp, &mnt) != -1) { if (tr_strcmp0(path, mnt.mnt_mountp) == 0) @@ -105,15 +102,13 @@ static char const* getdev(char const* path) #else - struct mntent const* mnt; - - fp = setmntent(_PATH_MOUNTED, "r"); - + FILE* const fp = setmntent(_PATH_MOUNTED, "r"); if (fp == nullptr) { return nullptr; } + struct mntent const* mnt = nullptr; while ((mnt = getmntent(fp)) != nullptr) { if (tr_strcmp0(path, mnt->mnt_dir) == 0) @@ -129,11 +124,8 @@ static char const* getdev(char const* path) #else /* BSD derived systems */ - int n; - struct statfs* mnt; - - n = getmntinfo(&mnt, MNT_WAIT); - + struct statfs* mnt = nullptr; + int const n = getmntinfo(&mnt, MNT_WAIT); if (n == 0) { return nullptr; @@ -156,18 +148,15 @@ static char const* getfstype(char const* device) { #ifdef HAVE_GETMNTENT - FILE* fp; - #ifdef __sun - struct mnttab mnt; - fp = fopen(_PATH_MOUNTED, "r"); - + FILE* const fp = fopen(_PATH_MOUNTED, "r"); if (fp == nullptr) { return nullptr; } + struct mnttab mnt; while (getmntent(fp, &mnt) != -1) { if (tr_strcmp0(device, mnt.mnt_mountp) == 0) @@ -181,15 +170,13 @@ static char const* getfstype(char const* device) #else - struct mntent const* mnt; - - fp = setmntent(_PATH_MOUNTED, "r"); - + FILE* const fp = setmntent(_PATH_MOUNTED, "r"); if (fp == nullptr) { return nullptr; } + struct mntent const* mnt = nullptr; while ((mnt = getmntent(fp)) != nullptr) { if (tr_strcmp0(device, mnt->mnt_fsname) == 0) @@ -205,11 +192,8 @@ static char const* getfstype(char const* device) #else /* BSD derived systems */ - int n; - struct statfs* mnt; - - n = getmntinfo(&mnt, MNT_WAIT); - + struct statfs* mnt = nullptr; + int const n = getmntinfo(&mnt, MNT_WAIT); if (n == 0) { return nullptr; @@ -230,11 +214,9 @@ static char const* getfstype(char const* device) static char const* getblkdev(char const* path) { - char* c; - char* dir; - char const* device; + char const* device = nullptr; - dir = tr_strdup(path); + char* const dir = tr_strdup(path); for (;;) { @@ -245,8 +227,7 @@ static char const* getblkdev(char const* path) break; } - c = strrchr(dir, '/'); - + char* c = strrchr(dir, '/'); if (c != nullptr) { *c = '\0'; @@ -274,9 +255,6 @@ struct tr_disk_space getquota(char const* device) struct quotakey qk; struct quotaval qv; struct tr_disk_space disk_space = { -1, -1 }; - int64_t limit; - int64_t freespace; - int64_t spaceused; qh = quota_open(device); @@ -295,6 +273,7 @@ struct tr_disk_space getquota(char const* device) return disk_space; } + int64_t limit; if (qv.qv_softlimit > 0) { limit = qv.qv_softlimit; @@ -309,10 +288,10 @@ struct tr_disk_space getquota(char const* device) return disk_space; } - spaceused = qv.qv_usage; + int64_t const spaceused = qv.qv_usage; quota_close(qh); - freespace = limit - spaceused; + int64_t const freespace = limit - spaceused; disk_space.free = freespace < 0 ? 0 : freespace; disk_space.total = limit; return disk_space; @@ -327,10 +306,7 @@ static struct tr_disk_space getquota(char const* device) #else struct dqblk dq; #endif - int64_t limit; - int64_t freespace; struct tr_disk_space disk_space = { -1, -1 }; - int64_t spaceused; #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__) if (quotactl(device, QCMD(Q_GETQUOTA, USRQUOTA), getuid(), (caddr_t)&dq) != 0) @@ -364,6 +340,7 @@ static struct tr_disk_space getquota(char const* device) } #endif + int64_t limit = 0; if (dq.dqb_bsoftlimit > 0) { /* Use soft limit first */ @@ -380,18 +357,18 @@ static struct tr_disk_space getquota(char const* device) } #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) - spaceused = (int64_t)dq.dqb_curblocks >> 1; + int64_t const spaceused = (int64_t)dq.dqb_curblocks >> 1; #elif defined(__APPLE__) - spaceused = (int64_t)dq.dqb_curbytes; + int64_t const spaceused = (int64_t)dq.dqb_curbytes; #elif defined(__UCLIBC__) && !TR_UCLIBC_CHECK_VERSION(1, 0, 18) - spaceused = (int64_t)btodb(dq.dqb_curblocks); + int64_t const spaceused = (int64_t)btodb(dq.dqb_curblocks); #elif defined(__sun) || (defined(_LINUX_QUOTA_VERSION) && _LINUX_QUOTA_VERSION < 2) - spaceused = (int64_t)dq.dqb_curblocks >> 1; + int64_t const spaceused = (int64_t)dq.dqb_curblocks >> 1; #else - spaceused = btodb(dq.dqb_curspace); + int64_t const spaceused = btodb(dq.dqb_curspace); #endif - freespace = limit - spaceused; + int64_t const freespace = limit - spaceused; #ifdef __APPLE__ disk_space.free = freespace < 0 ? 0 : freespace; @@ -410,13 +387,12 @@ static struct tr_disk_space getquota(char const* device) static struct tr_disk_space getxfsquota(char* device) { - int64_t limit; - int64_t freespace; struct tr_disk_space disk_space = { -1, -1 }; struct fs_disk_quota dq; if (quotactl(QCMD(Q_XGETQUOTA, USRQUOTA), device, getuid(), (caddr_t)&dq) == 0) { + int64_t limit = 0; if (dq.d_blk_softlimit > 0) { /* Use soft limit first */ @@ -432,7 +408,7 @@ static struct tr_disk_space getxfsquota(char* device) return disk_space; } - freespace = limit - (dq.d_bcount >> 1); + int64_t freespace = limit - (dq.d_bcount >> 1); freespace = freespace < 0 ? 0 : (freespace * 1024); limit = limit * 1024; disk_space.free = freespace; @@ -475,10 +451,8 @@ static struct tr_disk_space tr_getDiskSpace(char const* path) #ifdef _WIN32 struct tr_disk_space ret = { -1, -1 }; - wchar_t* wide_path; - - wide_path = tr_win32_utf8_to_native(path, -1); + wchar_t* const wide_path = tr_win32_utf8_to_native(path, -1); if (wide_path != nullptr) { ULARGE_INTEGER freeBytesAvailable; @@ -513,9 +487,7 @@ static struct tr_disk_space tr_getDiskSpace(char const* path) struct tr_device_info* tr_device_info_create(char const* path) { - struct tr_device_info* info; - - info = tr_new0(struct tr_device_info, 1); + auto* const info = tr_new0(struct tr_device_info, 1); info->path = tr_strdup(path); #ifndef _WIN32 diff --git a/libtransmission/subprocess-posix.cc b/libtransmission/subprocess-posix.cc index dfe2dca1a..0d816249c 100644 --- a/libtransmission/subprocess-posix.cc +++ b/libtransmission/subprocess-posix.cc @@ -24,7 +24,7 @@ static void handle_sigchld(int /*i*/) { - int rc; + int rc = 0; do { @@ -84,8 +84,8 @@ FAIL: static bool tr_spawn_async_in_parent(int pipe_fd, tr_error** error) { - int child_errno; - ssize_t count; + int child_errno = 0; + ssize_t count = 0; static_assert(sizeof(child_errno) == sizeof(errno), "");