From b04c4124d4c4bd519ee81c7f0840c5859bd0eb5a Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Thu, 3 Aug 2017 21:42:16 -0700 Subject: [PATCH 01/10] modernize-use-nullptr: replace NULL, 0 with nullptr --- encfs/BlockFileIO.cpp | 4 +-- encfs/Cipher.cpp | 2 +- encfs/CipherFileIO.cpp | 2 +- encfs/Context.cpp | 12 +++---- encfs/DirNode.cpp | 39 +++++++++++---------- encfs/Error.cpp | 9 +++-- encfs/FileNode.cpp | 5 +-- encfs/FileUtils.cpp | 35 ++++++++++--------- encfs/MACFileIO.cpp | 2 +- encfs/MemoryPool.cpp | 12 +++---- encfs/NameIO.cpp | 14 ++++---- encfs/SSL_Cipher.cpp | 56 +++++++++++++++--------------- encfs/XmlReader.cpp | 8 ++--- encfs/autosprintf.cpp | 8 ++--- encfs/base64.cpp | 2 +- encfs/encfs.cpp | 42 ++++++++++++----------- encfs/main.cpp | 74 +++++++++++++++++++++------------------- encfs/openssl.cpp | 6 ++-- encfs/readpassphrase.cpp | 20 +++++------ encfs/test.cpp | 9 ++--- 20 files changed, 186 insertions(+), 175 deletions(-) diff --git a/encfs/BlockFileIO.cpp b/encfs/BlockFileIO.cpp index 032217d..06a39ae 100644 --- a/encfs/BlockFileIO.cpp +++ b/encfs/BlockFileIO.cpp @@ -131,7 +131,7 @@ ssize_t BlockFileIO::read(const IORequest &req) const { MemBlock mb; // in case we need to allocate a temporary block.. IORequest blockReq; // for requests we may need to make blockReq.dataLen = _blockSize; - blockReq.data = NULL; + blockReq.data = nullptr; unsigned char *out = req.data; while (size) { @@ -210,7 +210,7 @@ bool BlockFileIO::write(const IORequest &req) { MemBlock mb; IORequest blockReq; - blockReq.data = NULL; + blockReq.data = nullptr; blockReq.dataLen = _blockSize; bool ok = true; diff --git a/encfs/Cipher.cpp b/encfs/Cipher.cpp index 2e1bed7..8ae0f9a 100644 --- a/encfs/Cipher.cpp +++ b/encfs/Cipher.cpp @@ -57,7 +57,7 @@ struct CipherAlg { }; typedef multimap CipherMap_t; -static CipherMap_t *gCipherMap = NULL; +static CipherMap_t *gCipherMap = nullptr; std::list Cipher::GetAlgorithmList( bool includeHidden) { diff --git a/encfs/CipherFileIO.cpp b/encfs/CipherFileIO.cpp index 1a16dac..0179db9 100644 --- a/encfs/CipherFileIO.cpp +++ b/encfs/CipherFileIO.cpp @@ -441,7 +441,7 @@ int CipherFileIO::truncate(off_t size) { // can't let BlockFileIO call base->truncate(), since it would be using // the wrong size.. - res = BlockFileIO::truncateBase(size, 0); + res = BlockFileIO::truncateBase(size, nullptr); if (res == 0) base->truncate(size + HEADER_SIZE); } diff --git a/encfs/Context.cpp b/encfs/Context.cpp index 5585275..fbb8acb 100644 --- a/encfs/Context.cpp +++ b/encfs/Context.cpp @@ -29,9 +29,9 @@ namespace encfs { EncFS_Context::EncFS_Context() { - pthread_cond_init(&wakeupCond, 0); - pthread_mutex_init(&wakeupMutex, 0); - pthread_mutex_init(&contextMutex, 0); + pthread_cond_init(&wakeupCond, nullptr); + pthread_mutex_init(&wakeupMutex, nullptr); + pthread_mutex_init(&contextMutex, nullptr); usageCount = 0; currentFuseFh = 1; @@ -109,8 +109,7 @@ void EncFS_Context::renameNode(const char *from, const char *to) { // putNode stores "node" under key "path" in the "openFiles" map. It // increments the reference count if the key already exists. -void EncFS_Context::putNode(const char *path, - std::shared_ptr node) { +void EncFS_Context::putNode(const char *path, std::shared_ptr node) { Lock lock(contextMutex); auto &list = openFiles[std::string(path)]; // The length of "list" serves as the reference count. @@ -120,7 +119,8 @@ void EncFS_Context::putNode(const char *path, // eraseNode is called by encfs_release in response to the RELEASE // FUSE-command we get from the kernel. -void EncFS_Context::eraseNode(const char *path, std::shared_ptr fnode) { +void EncFS_Context::eraseNode(const char *path, + std::shared_ptr fnode) { Lock lock(contextMutex); FileMap::iterator it = openFiles.find(std::string(path)); diff --git a/encfs/DirNode.cpp b/encfs/DirNode.cpp index 1692fd9..5560b75 100644 --- a/encfs/DirNode.cpp +++ b/encfs/DirNode.cpp @@ -94,7 +94,7 @@ static bool _nextName(struct dirent *&de, const std::shared_ptr &dir, } std::string DirTraverse::nextPlaintextName(int *fileType, ino_t *inode) { - struct dirent *de = 0; + struct dirent *de = nullptr; while (_nextName(de, dir, fileType, inode)) { try { uint64_t localIv = iv; @@ -109,9 +109,9 @@ std::string DirTraverse::nextPlaintextName(int *fileType, ino_t *inode) { } std::string DirTraverse::nextInvalid() { - struct dirent *de = 0; + struct dirent *de = nullptr; // find the first name which produces a decoding error... - while (_nextName(de, dir, (int *)0, (ino_t *)0)) { + while (_nextName(de, dir, (int *)nullptr, (ino_t *)nullptr)) { try { uint64_t localIv = iv; naming->decodePath(de->d_name, &localIv); @@ -241,7 +241,7 @@ void RenameOp::undo() { DirNode::DirNode(EncFS_Context *_ctx, const string &sourceDir, const FSConfigPtr &_config) { - pthread_mutex_init(&mutex, 0); + pthread_mutex_init(&mutex, nullptr); Lock _lock(mutex); @@ -354,7 +354,7 @@ DirTraverse DirNode::openDir(const char *plaintextPath) { string cyName = rootDir + naming->encodePath(plaintextPath); DIR *dir = ::opendir(cyName.c_str()); - if (dir == NULL) { + if (dir == nullptr) { VLOG(1) << "opendir error " << strerror(errno); return DirTraverse(shared_ptr(), 0, std::shared_ptr()); } else { @@ -392,8 +392,8 @@ bool DirNode::genRenameList(list &renameList, const char *fromP, std::shared_ptr(opendir(sourcePath.c_str()), DirDeleter()); if (!dir) return false; - struct dirent *de = NULL; - while ((de = ::readdir(dir.get())) != NULL) { + struct dirent *de = nullptr; + while ((de = ::readdir(dir.get())) != nullptr) { // decode the name using the oldIV uint64_t localIV = fromIV; string plainName; @@ -475,8 +475,9 @@ bool DirNode::genRenameList(list &renameList, const char *fromP, will have changed.. Returns a list of renamed items on success, a null list on failure. -*/ std::shared_ptr DirNode::newRenameOp(const char *fromP, - const char *toP) { +*/ +std::shared_ptr DirNode::newRenameOp(const char *fromP, + const char *toP) { // Do the rename in two stages to avoid chasing our tail // Undo everything if we encounter an error! std::shared_ptr > renameList(new list); @@ -604,10 +605,12 @@ int DirNode::link(const char *from, const char *to) { /* The node is keyed by filename, so a rename means the internal node names must be changed. -*/ std::shared_ptr DirNode::renameNode(const char *from, - const char *to) { +*/ +std::shared_ptr DirNode::renameNode(const char *from, + const char *to) { return renameNode(from, to, true); } + std::shared_ptr DirNode::renameNode(const char *from, const char *to, bool forwardMode) { std::shared_ptr node = findOrCreate(from); @@ -637,18 +640,18 @@ std::shared_ptr DirNode::findOrCreate(const char *plainName) { std::shared_ptr node; // See if we already have a FileNode for this path. - if (ctx) - node = ctx->lookupNode(plainName); + if (ctx) node = ctx->lookupNode(plainName); // If we don't, create a new one. if (!node) { uint64_t iv = 0; string cipherName = naming->encodePath(plainName, &iv); - uint64_t fuseFh = ctx->nextFuseFh(); + uint64_t fuseFh = ctx->nextFuseFh(); node.reset(new FileNode(this, fsConfig, plainName, (rootDir + cipherName).c_str(), fuseFh)); - if (fsConfig->config->externalIVChaining) node->setName(0, 0, iv); + if (fsConfig->config->externalIVChaining) + node->setName(nullptr, nullptr, iv); VLOG(1) << "created FileNode for " << node->cipherName(); } @@ -669,10 +672,10 @@ shared_ptr DirNode::lookupNode(const char *plainName, "result" is set to -1 on failure, a value >= 0 on success. */ std::shared_ptr DirNode::openNode(const char *plainName, - const char *requestor, int flags, - int *result) { + const char *requestor, int flags, + int *result) { (void)requestor; - rAssert(result != NULL); + rAssert(result != nullptr); Lock _lock(mutex); std::shared_ptr node = findOrCreate(plainName); diff --git a/encfs/Error.cpp b/encfs/Error.cpp index 63ae113..7d75dea 100644 --- a/encfs/Error.cpp +++ b/encfs/Error.cpp @@ -18,18 +18,17 @@ void initLogging(bool enable_debug, bool is_daemon) { if (is_daemon) { prefix = ""; encfs::rlogAction = el::base::DispatchAction::SysLog; - } - else { + } else { el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput); } if (!enable_debug) { suffix = ""; defaultConf.set(el::Level::Debug, el::ConfigurationType::Enabled, "false"); - } - else { + } else { el::Loggers::setVerboseLevel(1); } - defaultConf.setGlobally(el::ConfigurationType::Format, prefix + std::string("%level %msg") + suffix); + defaultConf.setGlobally(el::ConfigurationType::Format, + prefix + std::string("%level %msg") + suffix); el::Loggers::reconfigureLogger("default", defaultConf); } diff --git a/encfs/FileNode.cpp b/encfs/FileNode.cpp index e407d3b..96e27b9 100644 --- a/encfs/FileNode.cpp +++ b/encfs/FileNode.cpp @@ -53,9 +53,10 @@ namespace encfs { */ FileNode::FileNode(DirNode *parent_, const FSConfigPtr &cfg, - const char *plaintextName_, const char *cipherName_, uint64_t fuseFh) { + const char *plaintextName_, const char *cipherName_, + uint64_t fuseFh) { - pthread_mutex_init(&mutex, 0); + pthread_mutex_init(&mutex, nullptr); Lock _lock(mutex); diff --git a/encfs/FileUtils.cpp b/encfs/FileUtils.cpp index f3a3eb8..ae4adf3 100644 --- a/encfs/FileUtils.cpp +++ b/encfs/FileUtils.cpp @@ -111,12 +111,12 @@ struct ConfigInfo { // backward compatible support for older versions {".encfs5", Config_V5, "ENCFS5_CONFIG", readV5Config, writeV5Config, V5SubVersion, V5SubVersionDefault}, - {".encfs4", Config_V4, NULL, readV4Config, writeV4Config, 0, 0}, + {".encfs4", Config_V4, nullptr, readV4Config, writeV4Config, 0, 0}, // no longer support earlier versions - {".encfs3", Config_V3, NULL, NULL, NULL, 0, 0}, - {".encfs2", Config_Prehistoric, NULL, NULL, NULL, 0, 0}, - {".encfs", Config_Prehistoric, NULL, NULL, NULL, 0, 0}, - {NULL, Config_None, NULL, NULL, NULL, 0, 0}}; + {".encfs3", Config_V3, nullptr, nullptr, nullptr, 0, 0}, + {".encfs2", Config_Prehistoric, nullptr, nullptr, nullptr, 0, 0}, + {".encfs", Config_Prehistoric, nullptr, nullptr, nullptr, 0, 0}, + {nullptr, Config_None, nullptr, nullptr, nullptr, 0, 0}}; EncFS_Root::EncFS_Root() {} @@ -188,7 +188,7 @@ bool userAllowMkdir(int promptno, const char *path, mode_t mode) { } res = fgets(answer, sizeof(answer), stdin); - if (res != 0 && toupper(answer[0]) == 'Y') { + if (res != nullptr && toupper(answer[0]) == 'Y') { int result = mkdir(path, mode); if (result < 0) { perror(_("Unable to create directory: ")); @@ -235,9 +235,9 @@ ConfigType readConfig(const string &rootDir, EncFSConfig *config) { ConfigInfo *nm = ConfigFileMapping; while (nm->fileName) { // allow environment variable to override default config path - if (nm->environmentOverride != NULL) { + if (nm->environmentOverride != nullptr) { char *envFile = getenv(nm->environmentOverride); - if (envFile != NULL) { + if (envFile != nullptr) { if (!fileExists(envFile)) { RLOG(ERROR) << "fatal: config file specified by environment does not exist: " @@ -448,10 +448,10 @@ bool saveConfig(ConfigType type, const string &rootDir, while (nm->fileName) { if (nm->type == type && nm->saveFunc) { string path = rootDir + nm->fileName; - if (nm->environmentOverride != NULL) { + if (nm->environmentOverride != nullptr) { // use environment file if specified.. const char *envFile = getenv(nm->environmentOverride); - if (envFile != NULL) path.assign(envFile); + if (envFile != nullptr) path.assign(envFile); } try { @@ -649,7 +649,7 @@ static Cipher::CipherAlgorithm selectCipherAlgorithm() { cout << "\n" << _("Enter the number corresponding to your choice: "); char answer[10]; char *res = fgets(answer, sizeof(answer), stdin); - int cipherNum = (res == 0 ? 0 : atoi(answer)); + int cipherNum = (res == nullptr ? 0 : atoi(answer)); cout << "\n"; if (cipherNum < 1 || cipherNum > (int)algorithms.size()) { @@ -692,7 +692,7 @@ static Interface selectNameCoding() { cout << "\n" << _("Enter the number corresponding to your choice: "); char answer[10]; char *res = fgets(answer, sizeof(answer), stdin); - int algNum = (res == 0 ? 0 : atoi(answer)); + int algNum = (res == nullptr ? 0 : atoi(answer)); cout << "\n"; if (algNum < 1 || algNum > (int)algorithms.size()) { @@ -754,7 +754,7 @@ static int selectKeySize(const Cipher::CipherAlgorithm &alg) { char answer[10]; char *res = fgets(answer, sizeof(answer), stdin); - int keySize = (res == 0 ? 0 : atoi(answer)); + int keySize = (res == nullptr ? 0 : atoi(answer)); cout << "\n"; keySize = alg.keyLength.closest(keySize); @@ -794,7 +794,8 @@ static int selectBlockSize(const Cipher::CipherAlgorithm &alg) { char *res = fgets(answer, sizeof(answer), stdin); cout << "\n"; - if (res != 0 && atoi(answer) >= alg.blockSize.min()) blockSize = atoi(answer); + if (res != nullptr && atoi(answer) >= alg.blockSize.min()) + blockSize = atoi(answer); blockSize = alg.blockSize.closest(blockSize); @@ -893,7 +894,7 @@ static void selectBlockMAC(int *macBytes, int *macRandBytes, bool forceMac) { char *res = fgets(answer, sizeof(answer), stdin); cout << "\n"; - randSize = (res == 0 ? 0 : atoi(answer)); + randSize = (res == nullptr ? 0 : atoi(answer)); if (randSize < 0) randSize = 0; if (randSize > 8) randSize = 8; @@ -1462,7 +1463,7 @@ CipherKey EncFSConfig::getUserKey(const std::string &passProg, argv[0] = "/bin/sh"; argv[1] = "-c"; argv[2] = passProg.c_str(); - argv[3] = 0; + argv[3] = nullptr; // child process.. run the command and send output to fds[0] close(fds[1]); // we don't use the other half.. @@ -1500,7 +1501,7 @@ CipherKey EncFSConfig::getUserKey(const std::string &passProg, string password = readPassword(fds[1]); close(fds[1]); - waitpid(pid, NULL, 0); + waitpid(pid, nullptr, 0); // convert to key.. result = makeKey(password.c_str(), password.length()); diff --git a/encfs/MACFileIO.cpp b/encfs/MACFileIO.cpp index cf2760c..092fb1f 100644 --- a/encfs/MACFileIO.cpp +++ b/encfs/MACFileIO.cpp @@ -251,7 +251,7 @@ int MACFileIO::truncate(off_t size) { int headerSize = macBytes + randBytes; int bs = blockSize() + headerSize; - int res = BlockFileIO::truncateBase(size, 0); + int res = BlockFileIO::truncateBase(size, nullptr); if (res == 0) base->truncate(locWithHeader(size, bs, headerSize)); diff --git a/encfs/MemoryPool.cpp b/encfs/MemoryPool.cpp index 532ac8c..37137bc 100644 --- a/encfs/MemoryPool.cpp +++ b/encfs/MemoryPool.cpp @@ -61,15 +61,15 @@ static void freeBlock(BlockList *el) { } static pthread_mutex_t gMPoolMutex = PTHREAD_MUTEX_INITIALIZER; -static BlockList *gMemPool = NULL; +static BlockList *gMemPool = nullptr; MemBlock MemoryPool::allocate(int size) { pthread_mutex_lock(&gMPoolMutex); - BlockList *parent = NULL; + BlockList *parent = nullptr; BlockList *block = gMemPool; // check if we already have a large enough block available.. - while (block != NULL && block->size < size) { + while (block != nullptr && block->size < size) { parent = block; block = block->next; } @@ -84,7 +84,7 @@ MemBlock MemoryPool::allocate(int size) { pthread_mutex_unlock(&gMPoolMutex); if (!block) block = allocBlock(size); - block->next = NULL; + block->next = nullptr; MemBlock result; result.data = BLOCKDATA(block); @@ -115,11 +115,11 @@ void MemoryPool::destroyAll() { pthread_mutex_lock(&gMPoolMutex); BlockList *block = gMemPool; - gMemPool = NULL; + gMemPool = nullptr; pthread_mutex_unlock(&gMPoolMutex); - while (block != NULL) { + while (block != nullptr) { BlockList *next = block->next; freeBlock(block); diff --git a/encfs/NameIO.cpp b/encfs/NameIO.cpp index 6495586..7dbf9dc 100644 --- a/encfs/NameIO.cpp +++ b/encfs/NameIO.cpp @@ -56,7 +56,7 @@ struct NameIOAlg { }; typedef multimap NameIOMap_t; -static NameIOMap_t *gNameIOMap = 0; +static NameIOMap_t *gNameIOMap = nullptr; list NameIO::GetAlgorithmList(bool includeHidden) { AddSymbolReferences(); @@ -195,14 +195,14 @@ std::string NameIO::decodePath(const char *cipherPath) const { std::string NameIO::_encodePath(const char *plaintextPath, uint64_t *iv) const { // if chaining is not enabled, then the iv pointer is not used.. - if (!chainedNameIV) iv = 0; + if (!chainedNameIV) iv = nullptr; return recodePath(plaintextPath, &NameIO::maxEncodedNameLen, &NameIO::encodeName, iv); } std::string NameIO::_decodePath(const char *cipherPath, uint64_t *iv) const { // if chaining is not enabled, then the iv pointer is not used.. - if (!chainedNameIV) iv = 0; + if (!chainedNameIV) iv = nullptr; return recodePath(cipherPath, &NameIO::maxDecodedNameLen, &NameIO::decodeName, iv); } @@ -217,12 +217,12 @@ std::string NameIO::decodePath(const char *path, uint64_t *iv) const { int NameIO::encodeName(const char *input, int length, char *output, int bufferLength) const { - return encodeName(input, length, (uint64_t *)0, output, bufferLength); + return encodeName(input, length, (uint64_t *)nullptr, output, bufferLength); } int NameIO::decodeName(const char *input, int length, char *output, int bufferLength) const { - return decodeName(input, length, (uint64_t *)0, output, bufferLength); + return decodeName(input, length, (uint64_t *)nullptr, output, bufferLength); } std::string NameIO::_encodeName(const char *plaintextName, int length) const { @@ -232,7 +232,7 @@ std::string NameIO::_encodeName(const char *plaintextName, int length) const { BUFFER_INIT_S(codeBuf, 32, (unsigned int)approxLen + 1, bufSize) // code the name - int codedLen = encodeName(plaintextName, length, 0, codeBuf, bufSize); + int codedLen = encodeName(plaintextName, length, nullptr, codeBuf, bufSize); rAssert(codedLen <= approxLen); rAssert(codeBuf[codedLen] == '\0'); @@ -251,7 +251,7 @@ std::string NameIO::_decodeName(const char *encodedName, int length) const { BUFFER_INIT_S(codeBuf, 32, (unsigned int)approxLen + 1, bufSize) // code the name - int codedLen = decodeName(encodedName, length, 0, codeBuf, bufSize); + int codedLen = decodeName(encodedName, length, nullptr, codeBuf, bufSize); rAssert(codedLen <= approxLen); rAssert(codeBuf[codedLen] == '\0'); diff --git a/encfs/SSL_Cipher.cpp b/encfs/SSL_Cipher.cpp index f2f8294..51e5ccf 100644 --- a/encfs/SSL_Cipher.cpp +++ b/encfs/SSL_Cipher.cpp @@ -36,8 +36,8 @@ #include "Interface.h" #include "Mutex.h" #include "Range.h" -#include "SSL_Compat.h" #include "SSL_Cipher.h" +#include "SSL_Compat.h" #include "intl/gettext.h" using namespace std; @@ -66,7 +66,7 @@ inline int MIN(int a, int b) { return (a < b) ? a : b; } int BytesToKey(int keyLen, int ivLen, const EVP_MD *md, const unsigned char *data, int dataLen, unsigned int rounds, unsigned char *key, unsigned char *iv) { - if (data == NULL || dataLen == 0) + if (data == nullptr || dataLen == 0) return 0; // OpenSSL returns nkey here, but why? It is a failure.. unsigned char mdBuf[EVP_MAX_MD_SIZE]; @@ -79,13 +79,13 @@ int BytesToKey(int keyLen, int ivLen, const EVP_MD *md, EVP_MD_CTX_init(cx); for (;;) { - EVP_DigestInit_ex(cx, md, NULL); + EVP_DigestInit_ex(cx, md, nullptr); if (addmd++) EVP_DigestUpdate(cx, mdBuf, mds); EVP_DigestUpdate(cx, data, dataLen); EVP_DigestFinal_ex(cx, mdBuf, &mds); for (unsigned int i = 1; i < rounds; ++i) { - EVP_DigestInit_ex(cx, md, NULL); + EVP_DigestInit_ex(cx, md, nullptr); EVP_DigestUpdate(cx, mdBuf, mds); EVP_DigestFinal_ex(cx, mdBuf, &mds); } @@ -125,13 +125,13 @@ int TimedPBKDF2(const char *pass, int passlen, const unsigned char *salt, timeval start, end; for (;;) { - gettimeofday(&start, 0); + gettimeofday(&start, nullptr); int res = PKCS5_PBKDF2_HMAC_SHA1(pass, passlen, const_cast(salt), saltlen, iter, keylen, out); if (res != 1) return -1; - gettimeofday(&end, 0); + gettimeofday(&end, nullptr); long delta = time_diff(end, start); if (delta < desiredPDFTime / 8) { @@ -189,8 +189,8 @@ static std::shared_ptr NewAESCipher(const Interface &iface, keyLen = AESKeyRange.closest(keyLen); - const EVP_CIPHER *blockCipher = 0; - const EVP_CIPHER *streamCipher = 0; + const EVP_CIPHER *blockCipher = nullptr; + const EVP_CIPHER *streamCipher = nullptr; switch (keyLen) { case 128: @@ -244,7 +244,7 @@ class SSLKey : public AbstractCipherKey { SSLKey::SSLKey(int keySize_, int ivLength_) { this->keySize = keySize_; this->ivLength = ivLength_; - pthread_mutex_init(&mutex, 0); + pthread_mutex_init(&mutex, nullptr); buffer = (unsigned char *)OPENSSL_malloc(keySize + ivLength); memset(buffer, 0, keySize + ivLength); @@ -272,7 +272,7 @@ SSLKey::~SSLKey() { keySize = 0; ivLength = 0; - buffer = 0; + buffer = nullptr; EVP_CIPHER_CTX_free(block_enc); EVP_CIPHER_CTX_free(block_dec); @@ -295,10 +295,10 @@ void initKey(const std::shared_ptr &key, const EVP_CIPHER *_blockCipher, Lock lock(key->mutex); // initialize the cipher context once so that we don't have to do it for // every block.. - EVP_EncryptInit_ex(key->block_enc, _blockCipher, NULL, NULL, NULL); - EVP_DecryptInit_ex(key->block_dec, _blockCipher, NULL, NULL, NULL); - EVP_EncryptInit_ex(key->stream_enc, _streamCipher, NULL, NULL, NULL); - EVP_DecryptInit_ex(key->stream_dec, _streamCipher, NULL, NULL, NULL); + EVP_EncryptInit_ex(key->block_enc, _blockCipher, nullptr, nullptr, nullptr); + EVP_DecryptInit_ex(key->block_dec, _blockCipher, nullptr, nullptr, nullptr); + EVP_EncryptInit_ex(key->stream_enc, _streamCipher, nullptr, nullptr, nullptr); + EVP_DecryptInit_ex(key->stream_dec, _streamCipher, nullptr, nullptr, nullptr); EVP_CIPHER_CTX_set_key_length(key->block_enc, _keySize); EVP_CIPHER_CTX_set_key_length(key->block_dec, _keySize); @@ -310,12 +310,12 @@ void initKey(const std::shared_ptr &key, const EVP_CIPHER *_blockCipher, EVP_CIPHER_CTX_set_padding(key->stream_enc, 0); EVP_CIPHER_CTX_set_padding(key->stream_dec, 0); - EVP_EncryptInit_ex(key->block_enc, NULL, NULL, KeyData(key), NULL); - EVP_DecryptInit_ex(key->block_dec, NULL, NULL, KeyData(key), NULL); - EVP_EncryptInit_ex(key->stream_enc, NULL, NULL, KeyData(key), NULL); - EVP_DecryptInit_ex(key->stream_dec, NULL, NULL, KeyData(key), NULL); + EVP_EncryptInit_ex(key->block_enc, nullptr, nullptr, KeyData(key), nullptr); + EVP_DecryptInit_ex(key->block_dec, nullptr, nullptr, KeyData(key), nullptr); + EVP_EncryptInit_ex(key->stream_enc, nullptr, nullptr, KeyData(key), nullptr); + EVP_DecryptInit_ex(key->stream_dec, nullptr, nullptr, KeyData(key), nullptr); - HMAC_Init_ex(key->mac_ctx, KeyData(key), _keySize, EVP_sha1(), 0); + HMAC_Init_ex(key->mac_ctx, KeyData(key), _keySize, EVP_sha1(), nullptr); } SSL_Cipher::SSL_Cipher(const Interface &iface_, const Interface &realIface_, @@ -401,7 +401,7 @@ CipherKey SSL_Cipher::newKey(const char *password, int passwdLength) { } } else { // for backward compatibility with filesystems created with 1:0 - bytes = EVP_BytesToKey(_blockCipher, EVP_sha1(), NULL, + bytes = EVP_BytesToKey(_blockCipher, EVP_sha1(), nullptr, (unsigned char *)password, passwdLength, 16, KeyData(key), IVData(key)); } @@ -456,7 +456,7 @@ static uint64_t _checksum_64(SSLKey *key, const unsigned char *data, unsigned char md[EVP_MAX_MD_SIZE]; unsigned int mdLen = EVP_MAX_MD_SIZE; - HMAC_Init_ex(key->mac_ctx, 0, 0, 0, 0); + HMAC_Init_ex(key->mac_ctx, nullptr, 0, nullptr, nullptr); HMAC_Update(key->mac_ctx, data, dataLen); if (chainedIV) { // toss in the chained IV as well @@ -637,7 +637,7 @@ void SSL_Cipher::setIVec(unsigned char *ivec, uint64_t seed, } // combine ivec and seed with HMAC - HMAC_Init_ex(key->mac_ctx, 0, 0, 0, 0); + HMAC_Init_ex(key->mac_ctx, nullptr, 0, nullptr, nullptr); HMAC_Update(key->mac_ctx, ivec, _ivLength); HMAC_Update(key->mac_ctx, md, 8); HMAC_Final(key->mac_ctx, md, &mdLen); @@ -732,7 +732,7 @@ bool SSL_Cipher::streamEncode(unsigned char *buf, int size, uint64_t iv64, shuffleBytes(buf, size); setIVec(ivec, iv64, key); - EVP_EncryptInit_ex(key->stream_enc, NULL, NULL, NULL, ivec); + EVP_EncryptInit_ex(key->stream_enc, nullptr, nullptr, nullptr, ivec); EVP_EncryptUpdate(key->stream_enc, buf, &dstLen, buf, size); EVP_EncryptFinal_ex(key->stream_enc, buf + dstLen, &tmpLen); @@ -740,7 +740,7 @@ bool SSL_Cipher::streamEncode(unsigned char *buf, int size, uint64_t iv64, shuffleBytes(buf, size); setIVec(ivec, iv64 + 1, key); - EVP_EncryptInit_ex(key->stream_enc, NULL, NULL, NULL, ivec); + EVP_EncryptInit_ex(key->stream_enc, nullptr, nullptr, nullptr, ivec); EVP_EncryptUpdate(key->stream_enc, buf, &dstLen, buf, size); EVP_EncryptFinal_ex(key->stream_enc, buf + dstLen, &tmpLen); @@ -766,7 +766,7 @@ bool SSL_Cipher::streamDecode(unsigned char *buf, int size, uint64_t iv64, int dstLen = 0, tmpLen = 0; setIVec(ivec, iv64 + 1, key); - EVP_DecryptInit_ex(key->stream_dec, NULL, NULL, NULL, ivec); + EVP_DecryptInit_ex(key->stream_dec, nullptr, nullptr, nullptr, ivec); EVP_DecryptUpdate(key->stream_dec, buf, &dstLen, buf, size); EVP_DecryptFinal_ex(key->stream_dec, buf + dstLen, &tmpLen); @@ -774,7 +774,7 @@ bool SSL_Cipher::streamDecode(unsigned char *buf, int size, uint64_t iv64, flipBytes(buf, size); setIVec(ivec, iv64, key); - EVP_DecryptInit_ex(key->stream_dec, NULL, NULL, NULL, ivec); + EVP_DecryptInit_ex(key->stream_dec, nullptr, nullptr, nullptr, ivec); EVP_DecryptUpdate(key->stream_dec, buf, &dstLen, buf, size); EVP_DecryptFinal_ex(key->stream_dec, buf + dstLen, &tmpLen); @@ -808,7 +808,7 @@ bool SSL_Cipher::blockEncode(unsigned char *buf, int size, uint64_t iv64, int dstLen = 0, tmpLen = 0; setIVec(ivec, iv64, key); - EVP_EncryptInit_ex(key->block_enc, NULL, NULL, NULL, ivec); + EVP_EncryptInit_ex(key->block_enc, nullptr, nullptr, nullptr, ivec); EVP_EncryptUpdate(key->block_enc, buf, &dstLen, buf, size); EVP_EncryptFinal_ex(key->block_enc, buf + dstLen, &tmpLen); dstLen += tmpLen; @@ -840,7 +840,7 @@ bool SSL_Cipher::blockDecode(unsigned char *buf, int size, uint64_t iv64, int dstLen = 0, tmpLen = 0; setIVec(ivec, iv64, key); - EVP_DecryptInit_ex(key->block_dec, NULL, NULL, NULL, ivec); + EVP_DecryptInit_ex(key->block_dec, nullptr, nullptr, nullptr, ivec); EVP_DecryptUpdate(key->block_dec, buf, &dstLen, buf, size); EVP_DecryptFinal_ex(key->block_dec, buf + dstLen, &tmpLen); dstLen += tmpLen; diff --git a/encfs/XmlReader.cpp b/encfs/XmlReader.cpp index 285a7ef..8016d9c 100644 --- a/encfs/XmlReader.cpp +++ b/encfs/XmlReader.cpp @@ -22,8 +22,8 @@ #include // for remove_if #include // for NULL -#include // for shared_ptr #include // for ifstream +#include // for shared_ptr #include // for ostringstream #include // for XMLElement, XMLNode, XMLDocument (ptr only) @@ -121,7 +121,7 @@ bool XmlValue::read(const char *path, Interface *out) const { std::string safeValueForNode(const tinyxml2::XMLElement *element) { std::string value; - if (element == NULL) return value; + if (element == nullptr) return value; const tinyxml2::XMLNode *child = element->FirstChild(); if (child) { @@ -180,13 +180,13 @@ bool XmlReader::load(const char *fileName) { XmlValuePtr XmlReader::operator[](const char *name) const { tinyxml2::XMLNode *node = pd->doc->FirstChildElement(name); - if (node == NULL) { + if (node == nullptr) { RLOG(ERROR) << "Xml node " << name << " not found"; return XmlValuePtr(new XmlValue()); } tinyxml2::XMLElement *element = node->ToElement(); - if (element == NULL) { + if (element == nullptr) { RLOG(ERROR) << "Xml node " << name << " not element"; return XmlValuePtr(new XmlValue()); } diff --git a/encfs/autosprintf.cpp b/encfs/autosprintf.cpp index c32aa8f..7281825 100644 --- a/encfs/autosprintf.cpp +++ b/encfs/autosprintf.cpp @@ -38,13 +38,13 @@ namespace gnu { autosprintf::autosprintf(const char *format, ...) { va_list args; va_start(args, format); - if (vasprintf(&str, format, args) < 0) str = NULL; + if (vasprintf(&str, format, args) < 0) str = nullptr; va_end(args); } /* Copy constructor. Necessary because the destructor is nontrivial. */ autosprintf::autosprintf(const autosprintf &src) { - str = (src.str != NULL ? strdup(src.str) : NULL); + str = (src.str != nullptr ? strdup(src.str) : nullptr); } /* Destructor: frees the temporarily allocated string. */ @@ -52,13 +52,13 @@ autosprintf::~autosprintf() { free(str); } /* Conversion to string. */ autosprintf::operator char *() const { - if (str != NULL) { + if (str != nullptr) { size_t length = strlen(str) + 1; char *copy = new char[length]; memcpy(copy, str, length); return copy; } else - return NULL; + return nullptr; } autosprintf::operator std::string() const { return std::string(str ? str : "(error in autosprintf)"); diff --git a/encfs/base64.cpp b/encfs/base64.cpp index 1c69164..cc6eb19 100644 --- a/encfs/base64.cpp +++ b/encfs/base64.cpp @@ -105,7 +105,7 @@ static void changeBase2Inline(unsigned char *src, int srcLen, int src2Pow, void changeBase2Inline(unsigned char *src, int srcLen, int src2Pow, int dst2Pow, bool outputPartialLastByte) { changeBase2Inline(src, srcLen, src2Pow, dst2Pow, outputPartialLastByte, 0, 0, - 0); + nullptr); } // character set for ascii b64: diff --git a/encfs/encfs.cpp b/encfs/encfs.cpp index 65993be..855863f 100644 --- a/encfs/encfs.cpp +++ b/encfs/encfs.cpp @@ -76,7 +76,7 @@ static EncFS_Context *context() { * if the argument is NULL. */ static bool isReadOnly(EncFS_Context *ctx) { - if (ctx == NULL) ctx = (EncFS_Context *)fuse_get_context()->private_data; + if (ctx == nullptr) ctx = (EncFS_Context *)fuse_get_context()->private_data; return ctx->opts->readOnly; } @@ -112,20 +112,22 @@ static int withCipherPath(const char *opName, const char *path, } static void checkCanary(std::shared_ptr fnode) { - if(fnode->canary == CANARY_OK) { + if (fnode->canary == CANARY_OK) { return; } - if(fnode->canary == CANARY_RELEASED) { + if (fnode->canary == CANARY_RELEASED) { // "fnode" may have been released after it was retrieved by // lookupFuseFh. This is not an error. std::shared_ptr will release // the memory only when all operations on the FileNode have been // completed. return; } - if(fnode->canary == CANARY_DESTROYED) { - RLOG(ERROR) << "canary=CANARY_DESTROYED. FileNode accessed after it was destroyed."; + if (fnode->canary == CANARY_DESTROYED) { + RLOG(ERROR) + << "canary=CANARY_DESTROYED. FileNode accessed after it was destroyed."; } else { - RLOG(ERROR) << "canary=0x" << std::hex << fnode->canary << ". Memory corruption?"; + RLOG(ERROR) << "canary=0x" << std::hex << fnode->canary + << ". Memory corruption?"; } throw Error("dead canary"); } @@ -214,7 +216,7 @@ int _do_getattr(FileNode *fnode, struct stat *stbuf) { } int encfs_getattr(const char *path, struct stat *stbuf) { - return withFileNode("getattr", path, NULL, bind(_do_getattr, _1, stbuf)); + return withFileNode("getattr", path, nullptr, bind(_do_getattr, _1, stbuf)); } int encfs_fgetattr(const char *path, struct stat *stbuf, @@ -366,7 +368,7 @@ int _do_rmdir(EncFS_Context *, const string &cipherPath) { } int encfs_rmdir(const char *path) { - if (isReadOnly(NULL)) return -EROFS; + if (isReadOnly(nullptr)) return -EROFS; return withCipherPath("rmdir", path, bind(_do_rmdir, _1, _2)); } @@ -485,7 +487,7 @@ int _do_chmod(EncFS_Context *, const string &cipherPath, mode_t mode) { } int encfs_chmod(const char *path, mode_t mode) { - if (isReadOnly(NULL)) return -EROFS; + if (isReadOnly(nullptr)) return -EROFS; return withCipherPath("chmod", path, bind(_do_chmod, _1, _2, mode)); } @@ -495,19 +497,19 @@ int _do_chown(EncFS_Context *, const string &cyName, uid_t u, gid_t g) { } int encfs_chown(const char *path, uid_t uid, gid_t gid) { - if (isReadOnly(NULL)) return -EROFS; + if (isReadOnly(nullptr)) return -EROFS; return withCipherPath("chown", path, bind(_do_chown, _1, _2, uid, gid)); } int _do_truncate(FileNode *fnode, off_t size) { return fnode->truncate(size); } int encfs_truncate(const char *path, off_t size) { - if (isReadOnly(NULL)) return -EROFS; - return withFileNode("truncate", path, NULL, bind(_do_truncate, _1, size)); + if (isReadOnly(nullptr)) return -EROFS; + return withFileNode("truncate", path, nullptr, bind(_do_truncate, _1, size)); } int encfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) { - if (isReadOnly(NULL)) return -EROFS; + if (isReadOnly(nullptr)) return -EROFS; return withFileNode("ftruncate", path, fi, bind(_do_truncate, _1, size)); } @@ -517,7 +519,7 @@ int _do_utime(EncFS_Context *, const string &cyName, struct utimbuf *buf) { } int encfs_utime(const char *path, struct utimbuf *buf) { - if (isReadOnly(NULL)) return -EROFS; + if (isReadOnly(nullptr)) return -EROFS; return withCipherPath("utime", path, bind(_do_utime, _1, _2, buf)); } @@ -538,7 +540,7 @@ int _do_utimens(EncFS_Context *, const string &cyName, } int encfs_utimens(const char *path, const struct timespec ts[2]) { - if (isReadOnly(NULL)) return -EROFS; + if (isReadOnly(nullptr)) return -EROFS; return withCipherPath("utimens", path, bind(_do_utimens, _1, _2, ts)); } @@ -641,7 +643,7 @@ int _do_fsync(FileNode *fnode, int dataSync) { } int encfs_fsync(const char *path, int dataSync, struct fuse_file_info *file) { - if (isReadOnly(NULL)) return -EROFS; + if (isReadOnly(nullptr)) return -EROFS; return withFileNode("fsync", path, file, bind(_do_fsync, _1, dataSync)); } @@ -654,7 +656,7 @@ int _do_write(FileNode *fnode, unsigned char *ptr, size_t size, off_t offset) { int encfs_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *file) { - if (isReadOnly(NULL)) return -EROFS; + if (isReadOnly(nullptr)) return -EROFS; return withFileNode("write", path, file, bind(_do_write, _1, (unsigned char *)buf, size, offset)); } @@ -666,7 +668,7 @@ int encfs_statfs(const char *path, struct statvfs *st) { int res = -EIO; try { (void)path; // path should always be '/' for now.. - rAssert(st != NULL); + rAssert(st != nullptr); string cyName = ctx->rootCipherDir; VLOG(1) << "doing statfs of " << cyName; @@ -704,7 +706,7 @@ int _do_setxattr(EncFS_Context *, const string &cyName, const char *name, } int encfs_setxattr(const char *path, const char *name, const char *value, size_t size, int flags) { - if (isReadOnly(NULL)) return -EROFS; + if (isReadOnly(nullptr)) return -EROFS; return withCipherPath("setxattr", path, bind(_do_setxattr, _1, _2, name, value, size, flags)); } @@ -762,7 +764,7 @@ int _do_removexattr(EncFS_Context *, const string &cyName, const char *name) { } int encfs_removexattr(const char *path, const char *name) { - if (isReadOnly(NULL)) return -EROFS; + if (isReadOnly(nullptr)) return -EROFS; return withCipherPath("removexattr", path, bind(_do_removexattr, _1, _2, name)); diff --git a/encfs/main.cpp b/encfs/main.cpp index 93b6a50..3b10884 100644 --- a/encfs/main.cpp +++ b/encfs/main.cpp @@ -161,7 +161,8 @@ static void FuseUsage() { int argc = 2; const char *argv[] = {"...", "-h"}; - fuse_main(argc, const_cast(argv), (fuse_operations *)NULL, NULL); + fuse_main(argc, const_cast(argv), (fuse_operations *)nullptr, + nullptr); } #define PUSHARG(ARG) \ @@ -202,35 +203,35 @@ static bool processArgs(int argc, char *argv[], // leave a space for mount point, as FUSE expects the mount point before // any flags - out->fuseArgv[1] = NULL; + out->fuseArgv[1] = nullptr; ++out->fuseArgc; // TODO: can flags be internationalized? static struct option long_options[] = { - {"fuse-debug", 0, 0, 'd'}, // Fuse debug mode - {"forcedecode", 0, 0, 'D'}, // force decode + {"fuse-debug", 0, nullptr, 'd'}, // Fuse debug mode + {"forcedecode", 0, nullptr, 'D'}, // force decode // {"foreground", 0, 0, 'f'}, // foreground mode (no daemon) - {"fuse-help", 0, 0, 'H'}, // fuse_mount usage - {"idle", 1, 0, 'i'}, // idle timeout - {"anykey", 0, 0, 'k'}, // skip key checks - {"no-default-flags", 0, 0, 'N'}, // don't use default fuse flags - {"ondemand", 0, 0, 'm'}, // mount on-demand - {"delaymount", 0, 0, 'M'}, // delay initial mount until use - {"public", 0, 0, 'P'}, // public mode - {"extpass", 1, 0, 'p'}, // external password program + {"fuse-help", 0, nullptr, 'H'}, // fuse_mount usage + {"idle", 1, nullptr, 'i'}, // idle timeout + {"anykey", 0, nullptr, 'k'}, // skip key checks + {"no-default-flags", 0, nullptr, 'N'}, // don't use default fuse flags + {"ondemand", 0, nullptr, 'm'}, // mount on-demand + {"delaymount", 0, nullptr, 'M'}, // delay initial mount until use + {"public", 0, nullptr, 'P'}, // public mode + {"extpass", 1, nullptr, 'p'}, // external password program // {"single-thread", 0, 0, 's'}, // single-threaded mode - {"stdinpass", 0, 0, 'S'}, // read password from stdin - {"syslogtag", 1, 0, 't'}, // syslog tag - {"annotate", 0, 0, - LONG_OPT_ANNOTATE}, // Print annotation lines to stderr - {"nocache", 0, 0, LONG_OPT_NOCACHE}, // disable caching - {"verbose", 0, 0, 'v'}, // verbose mode - {"version", 0, 0, 'V'}, // version - {"reverse", 0, 0, 'r'}, // reverse encryption - {"standard", 0, 0, '1'}, // standard configuration - {"paranoia", 0, 0, '2'}, // standard configuration - {"require-macs", 0, 0, LONG_OPT_REQUIRE_MAC}, // require MACs - {0, 0, 0, 0}}; + {"stdinpass", 0, nullptr, 'S'}, // read password from stdin + {"syslogtag", 1, nullptr, 't'}, // syslog tag + {"annotate", 0, nullptr, + LONG_OPT_ANNOTATE}, // Print annotation lines to stderr + {"nocache", 0, nullptr, LONG_OPT_NOCACHE}, // disable caching + {"verbose", 0, nullptr, 'v'}, // verbose mode + {"version", 0, nullptr, 'V'}, // version + {"reverse", 0, nullptr, 'r'}, // reverse encryption + {"standard", 0, nullptr, '1'}, // standard configuration + {"paranoia", 0, nullptr, '2'}, // standard configuration + {"require-macs", 0, nullptr, LONG_OPT_REQUIRE_MAC}, // require MACs + {nullptr, 0, nullptr, 0}}; while (1) { int option_index = 0; @@ -283,7 +284,7 @@ static bool processArgs(int argc, char *argv[], PUSHARG("-d"); break; case 'i': - out->idleTimeout = strtol(optarg, (char **)NULL, 10); + out->idleTimeout = strtol(optarg, (char **)nullptr, 10); out->opts->idleTracking = true; break; case 'k': @@ -358,7 +359,7 @@ static bool processArgs(int argc, char *argv[], #if defined(HAVE_XATTR) // "--verbose" has to be passed before "--version" for this to work. if (out->isVerbose) { - cerr << "Compiled with : HAVE_XATTR" << endl; + cerr << "Compiled with : HAVE_XATTR" << endl; } #endif exit(EXIT_SUCCESS); @@ -503,7 +504,8 @@ void *encfs_init(fuse_conn_info *conn) { VLOG(1) << "starting idle monitoring thread"; ctx->running = true; - int res = pthread_create(&ctx->monitorThread, 0, idleMonitor, (void *)ctx); + int res = + pthread_create(&ctx->monitorThread, nullptr, idleMonitor, (void *)ctx); if (res != 0) { RLOG(ERROR) << "error starting idle monitor thread, " "res = " @@ -533,7 +535,7 @@ int main(int argc, char *argv[]) { // we've processed it and only allowed through what we support. std::shared_ptr encfsArgs(new EncFS_Args); for (int i = 0; i < MaxFuseArgs; ++i) - encfsArgs->fuseArgv[i] = NULL; // libfuse expects null args.. + encfsArgs->fuseArgv[i] = nullptr; // libfuse expects null args.. if (argc == 1 || !processArgs(argc, argv, encfsArgs)) { usage(argv[0]); @@ -681,7 +683,7 @@ int main(int argc, char *argv[]) { pthread_cond_signal(&ctx->wakeupCond); pthread_mutex_unlock(&ctx->wakeupMutex); VLOG(1) << "joining with idle monitoring thread"; - pthread_join(ctx->monitorThread, 0); + pthread_join(ctx->monitorThread, nullptr); VLOG(1) << "join done"; } } @@ -715,7 +717,8 @@ static void *idleMonitor(void *_arg) { bool unmountres = false; - // We will notify when FS will be unmounted, so notify that it has just been mounted + // We will notify when FS will be unmounted, so notify that it has just been + // mounted RLOG(INFO) << "Filesystem mounted: " << arg->opts->mountPoint; pthread_mutex_lock(&ctx->wakeupMutex); @@ -751,7 +754,7 @@ static void *idleMonitor(void *_arg) { << timeoutCycles; struct timeval currentTime; - gettimeofday(¤tTime, 0); + gettimeofday(¤tTime, nullptr); struct timespec wakeupTime; wakeupTime.tv_sec = currentTime.tv_sec + ActivityCheckInterval; wakeupTime.tv_nsec = currentTime.tv_usec * 1000; @@ -760,13 +763,14 @@ static void *idleMonitor(void *_arg) { pthread_mutex_unlock(&ctx->wakeupMutex); - // If we are here FS has been unmounted, so if we did not unmount ourselves (manual, kill...), notify + // If we are here FS has been unmounted, so if we did not unmount ourselves + // (manual, kill...), notify if (!unmountres) RLOG(INFO) << "Filesystem unmounted: " << arg->opts->mountPoint; VLOG(1) << "Idle monitoring thread exiting"; - return 0; + return nullptr; } static bool unmountFS(EncFS_Context *ctx) { @@ -778,9 +782,9 @@ static bool unmountFS(EncFS_Context *ctx) { ctx->setRoot(std::shared_ptr()); return false; } else { - // Time to unmount! +// Time to unmount! #if FUSE_USE_VERSION < 30 - fuse_unmount(arg->opts->mountPoint.c_str(), NULL); + fuse_unmount(arg->opts->mountPoint.c_str(), nullptr); #else fuse_unmount(fuse_get_context()->fuse); #endif diff --git a/encfs/openssl.cpp b/encfs/openssl.cpp index 801a105..d859f1c 100644 --- a/encfs/openssl.cpp +++ b/encfs/openssl.cpp @@ -37,7 +37,7 @@ namespace encfs { unsigned long pthreads_thread_id() { return (unsigned long)pthread_self(); } -static pthread_mutex_t *crypto_locks = NULL; +static pthread_mutex_t *crypto_locks = nullptr; void pthreads_locking_callback(int mode, int n, const char *caller_file, int caller_line) { (void)caller_file; @@ -47,7 +47,7 @@ void pthreads_locking_callback(int mode, int n, const char *caller_file, VLOG(1) << "Allocating " << CRYPTO_num_locks() << " locks for OpenSSL"; crypto_locks = new pthread_mutex_t[CRYPTO_num_locks()]; for (int i = 0; i < CRYPTO_num_locks(); ++i) - pthread_mutex_init(crypto_locks + i, 0); + pthread_mutex_init(crypto_locks + i, nullptr); } if (mode & CRYPTO_LOCK) { @@ -62,7 +62,7 @@ void pthreads_locking_cleanup() { for (int i = 0; i < CRYPTO_num_locks(); ++i) pthread_mutex_destroy(crypto_locks + i); delete[] crypto_locks; - crypto_locks = NULL; + crypto_locks = nullptr; } } diff --git a/encfs/readpassphrase.cpp b/encfs/readpassphrase.cpp index daf4a56..a439e67 100644 --- a/encfs/readpassphrase.cpp +++ b/encfs/readpassphrase.cpp @@ -76,7 +76,7 @@ char *readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags) { /* I suppose we could alloc on demand in this case (XXX). */ if (bufsiz == 0) { errno = EINVAL; - return (NULL); + return (nullptr); } restart: @@ -87,7 +87,7 @@ restart: if ((input = output = open(_PATH_TTY, O_RDWR)) == -1) { if (flags & RPP_REQUIRE_TTY) { errno = ENOTTY; - return (NULL); + return (nullptr); } input = STDIN_FILENO; output = STDERR_FILENO; @@ -142,13 +142,13 @@ restart: /* Restore old terminal settings and signals. */ if (memcmp(&term, &oterm, sizeof(term)) != 0) (void)tcsetattr(input, _T_FLUSH, &oterm); - (void)sigaction(SIGINT, &saveint, NULL); - (void)sigaction(SIGHUP, &savehup, NULL); - (void)sigaction(SIGQUIT, &savequit, NULL); - (void)sigaction(SIGTERM, &saveterm, NULL); - (void)sigaction(SIGTSTP, &savetstp, NULL); - (void)sigaction(SIGTTIN, &savettin, NULL); - (void)sigaction(SIGTTOU, &savettou, NULL); + (void)sigaction(SIGINT, &saveint, nullptr); + (void)sigaction(SIGHUP, &savehup, nullptr); + (void)sigaction(SIGQUIT, &savequit, nullptr); + (void)sigaction(SIGTERM, &saveterm, nullptr); + (void)sigaction(SIGTSTP, &savetstp, nullptr); + (void)sigaction(SIGTTIN, &savettin, nullptr); + (void)sigaction(SIGTTOU, &savettou, nullptr); if (input != STDIN_FILENO) (void)close(input); /* @@ -167,7 +167,7 @@ restart: } errno = save_errno; - return (nr == -1 ? NULL : buf); + return (nr == -1 ? nullptr : buf); } #endif /* HAVE_READPASSPHRASE */ diff --git a/encfs/test.cpp b/encfs/test.cpp index e850db3..dff8bbf 100644 --- a/encfs/test.cpp +++ b/encfs/test.cpp @@ -173,11 +173,12 @@ bool runTests(const std::shared_ptr &cipher, bool verbose) { cfg.assignKeyData(keyBuf, encodedKeySize); // save config - //Creation of a temporary file should be more platform independent. On c++17 we could use std::filesystem. + // Creation of a temporary file should be more platform independent. On + // c++17 we could use std::filesystem. string name = "/tmp/encfstestXXXXXX"; int tmpFd = mkstemp(&name[0]); rAssert(-1 != tmpFd); - //mkstemp opens the temporary file, but we only need its name -> close it + // mkstemp opens the temporary file, but we only need its name -> close it rAssert(0 == close(tmpFd)); { auto ok = writeV6Config(name.c_str(), &cfg); @@ -190,9 +191,9 @@ bool runTests(const std::shared_ptr &cipher, bool verbose) { auto ok = readV6Config(name.c_str(), &cfg2, nullptr); rAssert(ok == true); } - //delete the temporary file where we stored the config + // delete the temporary file where we stored the config rAssert(0 == unlink(name.c_str())); - + // check.. rAssert(cfg.cipherIface.implements(cfg2.cipherIface)); rAssert(cfg.keySize == cfg2.keySize); From 5a99506ea8b2911dcef8a43082ead006efdeb88f Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Thu, 3 Aug 2017 22:00:32 -0700 Subject: [PATCH 02/10] modernize: use C++ headers, auto, make_shared --- encfs/Cipher.cpp | 6 +++--- encfs/CipherFileIO.cpp | 4 ++-- encfs/ConfigReader.cpp | 4 ++-- encfs/ConfigVar.cpp | 4 ++-- encfs/Context.cpp | 6 +++--- encfs/DirNode.cpp | 4 ++-- encfs/Error.h | 2 +- encfs/FileNode.cpp | 4 ++-- encfs/FileUtils.cpp | 18 ++++++++---------- encfs/MACFileIO.cpp | 2 +- encfs/MemoryPool.cpp | 4 ++-- encfs/RawFileIO.cpp | 2 +- encfs/SSL_Cipher.cpp | 2 +- encfs/XmlReader.cpp | 6 +++--- encfs/autosprintf.cpp | 10 +++++----- encfs/base64.cpp | 4 ++-- encfs/encfs.cpp | 6 +++--- encfs/main.cpp | 10 +++++----- encfs/openssl.cpp | 2 +- 19 files changed, 49 insertions(+), 51 deletions(-) diff --git a/encfs/Cipher.cpp b/encfs/Cipher.cpp index 8ae0f9a..00b9a8d 100644 --- a/encfs/Cipher.cpp +++ b/encfs/Cipher.cpp @@ -18,10 +18,10 @@ * along with this program. If not, see . */ +#include #include #include #include -#include #include #include @@ -184,13 +184,13 @@ bool Cipher::nameDecode(unsigned char *data, int len, uint64_t iv64, string Cipher::encodeAsString(const CipherKey &key, const CipherKey &encodingKey) { int encodedKeySize = this->encodedKeySize(); - unsigned char *keyBuf = new unsigned char[encodedKeySize]; + auto *keyBuf = new unsigned char[encodedKeySize]; // write the key, encoding it with itself. this->writeKey(key, keyBuf, encodingKey); int b64Len = B256ToB64Bytes(encodedKeySize); - unsigned char *b64Key = new unsigned char[b64Len + 1]; + auto *b64Key = new unsigned char[b64Len + 1]; changeBase2(keyBuf, encodedKeySize, 8, b64Key, b64Len, 6); B64ToAscii(b64Key, b64Len); diff --git a/encfs/CipherFileIO.cpp b/encfs/CipherFileIO.cpp index 0179db9..40e3d5d 100644 --- a/encfs/CipherFileIO.cpp +++ b/encfs/CipherFileIO.cpp @@ -22,11 +22,11 @@ #include "internal/easylogging++.h" #include +#include +#include #include -#include #include #include -#include #include #include "BlockFileIO.h" diff --git a/encfs/ConfigReader.cpp b/encfs/ConfigReader.cpp index e622ecf..d3f52d1 100644 --- a/encfs/ConfigReader.cpp +++ b/encfs/ConfigReader.cpp @@ -49,7 +49,7 @@ bool ConfigReader::load(const char *fileName) { int fd = open(fileName, O_RDONLY); if (fd < 0) return false; - char *buf = new char[size]; + auto *buf = new char[size]; int res = ::read(fd, buf, size); close(fd); @@ -126,7 +126,7 @@ ConfigVar ConfigReader::toVar() const { ConfigVar ConfigReader::operator[](const std::string &varName) const { // read only - map::const_iterator it = vars.find(varName); + auto it = vars.find(varName); if (it == vars.end()) return ConfigVar(); else diff --git a/encfs/ConfigVar.cpp b/encfs/ConfigVar.cpp index a4fadc8..27792f1 100644 --- a/encfs/ConfigVar.cpp +++ b/encfs/ConfigVar.cpp @@ -112,7 +112,7 @@ void ConfigVar::writeInt(int val) { } int ConfigVar::readInt() const { - const unsigned char *buf = (const unsigned char *)buffer(); + const auto *buf = (const unsigned char *)buffer(); int bytes = this->size(); int offset = at(); int value = 0; @@ -184,7 +184,7 @@ const ConfigVar &operator>>(const ConfigVar &src, std::string &result) { unsigned char tmpBuf[32]; if (length > (int)sizeof(tmpBuf)) { - unsigned char *ptr = new unsigned char[length]; + auto *ptr = new unsigned char[length]; readLen = src.read(ptr, length); result.assign((char *)ptr, length); delete[] ptr; diff --git a/encfs/Context.cpp b/encfs/Context.cpp index fbb8acb..c6bf276 100644 --- a/encfs/Context.cpp +++ b/encfs/Context.cpp @@ -87,7 +87,7 @@ void EncFS_Context::getAndResetUsageCounter(int *usage, int *openCount) { std::shared_ptr EncFS_Context::lookupNode(const char *path) { Lock lock(contextMutex); - FileMap::iterator it = openFiles.find(std::string(path)); + auto it = openFiles.find(std::string(path)); if (it != openFiles.end()) { // every entry in the list is fine... so just use the // first @@ -99,7 +99,7 @@ std::shared_ptr EncFS_Context::lookupNode(const char *path) { void EncFS_Context::renameNode(const char *from, const char *to) { Lock lock(contextMutex); - FileMap::iterator it = openFiles.find(std::string(from)); + auto it = openFiles.find(std::string(from)); if (it != openFiles.end()) { auto val = it->second; openFiles.erase(it); @@ -123,7 +123,7 @@ void EncFS_Context::eraseNode(const char *path, std::shared_ptr fnode) { Lock lock(contextMutex); - FileMap::iterator it = openFiles.find(std::string(path)); + auto it = openFiles.find(std::string(path)); rAssert(it != openFiles.end()); auto &list = it->second; diff --git a/encfs/DirNode.cpp b/encfs/DirNode.cpp index 5560b75..f3d32e6 100644 --- a/encfs/DirNode.cpp +++ b/encfs/DirNode.cpp @@ -219,7 +219,7 @@ void RenameOp::undo() { // list has to be processed backwards, otherwise we may rename // directories and directory contents in the wrong order! int undoCount = 0; - list::const_iterator it = last; + auto it = last; while (it != renameList->begin()) { --it; @@ -485,7 +485,7 @@ std::shared_ptr DirNode::newRenameOp(const char *fromP, RLOG(WARNING) << "Error during generation of recursive rename list"; return std::shared_ptr(); } else - return std::shared_ptr(new RenameOp(this, renameList)); + return std::make_shared(this, renameList); } int DirNode::mkdir(const char *plaintextPath, mode_t mode, uid_t uid, diff --git a/encfs/Error.h b/encfs/Error.h index 3e54536..c045871 100644 --- a/encfs/Error.h +++ b/encfs/Error.h @@ -21,7 +21,7 @@ class Error : public std::runtime_error { RLOG(ERROR) << "Assert failed: " << STR(cond); \ throw encfs::Error(STR(cond)); \ } \ - } while (0) + } while (false) void initLogging(bool enable_debug = false, bool is_daemon = false); diff --git a/encfs/FileNode.cpp b/encfs/FileNode.cpp index 96e27b9..1565ef8 100644 --- a/encfs/FileNode.cpp +++ b/encfs/FileNode.cpp @@ -18,9 +18,9 @@ * along with this program. If not, see . */ -#include +#include +#include #include -#include #include #include #include diff --git a/encfs/FileUtils.cpp b/encfs/FileUtils.cpp index ae4adf3..a8c5b38 100644 --- a/encfs/FileUtils.cpp +++ b/encfs/FileUtils.cpp @@ -323,7 +323,7 @@ bool readV6Config(const char *configFile, EncFSConfig *cfg, ConfigInfo *info) { int encodedSize; config->read("encodedKeySize", &encodedSize); - unsigned char *key = new unsigned char[encodedSize]; + auto *key = new unsigned char[encodedSize]; config->readB64("encodedKeyData", key, encodedSize); cfg->assignKeyData(key, encodedSize); delete[] key; @@ -331,7 +331,7 @@ bool readV6Config(const char *configFile, EncFSConfig *cfg, ConfigInfo *info) { if (cfg->subVersion >= 20080816) { int saltLen; config->read("saltLen", &saltLen); - unsigned char *salt = new unsigned char[saltLen]; + auto *salt = new unsigned char[saltLen]; config->readB64("saltData", salt, saltLen); cfg->assignSaltData(salt, saltLen); delete[] salt; @@ -1161,7 +1161,7 @@ RootPtr createV6Config(EncFS_Context *ctx, "later using encfsctl.\n\n"); int encodedKeySize = cipher->encodedKeySize(); - unsigned char *encodedKey = new unsigned char[encodedKeySize]; + auto *encodedKey = new unsigned char[encodedKeySize]; CipherKey volumeKey = cipher->newRandomKey(); @@ -1216,11 +1216,10 @@ RootPtr createV6Config(EncFS_Context *ctx, fsConfig->idleTracking = enableIdleTracking; fsConfig->opts = opts; - rootInfo = RootPtr(new EncFS_Root); + rootInfo = std::make_shared(); rootInfo->cipher = cipher; rootInfo->volumeKey = volumeKey; - rootInfo->root = - std::shared_ptr(new DirNode(ctx, rootDir, fsConfig)); + rootInfo->root = std::make_shared(ctx, rootDir, fsConfig); return rootInfo; } @@ -1576,7 +1575,7 @@ RootPtr initFS(EncFS_Context *ctx, const std::shared_ptr &opts) { } if (opts->delayMount) { - rootInfo = RootPtr(new EncFS_Root); + rootInfo = std::make_shared(); rootInfo->cipher = cipher; rootInfo->root = std::shared_ptr(); return rootInfo; @@ -1632,11 +1631,10 @@ RootPtr initFS(EncFS_Context *ctx, const std::shared_ptr &opts) { fsConfig->reverseEncryption = opts->reverseEncryption; fsConfig->opts = opts; - rootInfo = RootPtr(new EncFS_Root); + rootInfo = std::make_shared(); rootInfo->cipher = cipher; rootInfo->volumeKey = volumeKey; - rootInfo->root = - std::shared_ptr(new DirNode(ctx, opts->rootDir, fsConfig)); + rootInfo->root = std::make_shared(ctx, opts->rootDir, fsConfig); } else { if (opts->createIfNotFound) { // creating a new encrypted filesystem diff --git a/encfs/MACFileIO.cpp b/encfs/MACFileIO.cpp index 092fb1f..6d2897f 100644 --- a/encfs/MACFileIO.cpp +++ b/encfs/MACFileIO.cpp @@ -21,8 +21,8 @@ #include "MACFileIO.h" #include "internal/easylogging++.h" +#include #include -#include #include #include "BlockFileIO.h" diff --git a/encfs/MemoryPool.cpp b/encfs/MemoryPool.cpp index 37137bc..6291785 100644 --- a/encfs/MemoryPool.cpp +++ b/encfs/MemoryPool.cpp @@ -44,7 +44,7 @@ struct BlockList { }; static BlockList *allocBlock(int size) { - BlockList *block = new BlockList; + auto *block = new BlockList; block->size = size; block->data = BUF_MEM_new(); BUF_MEM_grow(block->data, size); @@ -98,7 +98,7 @@ MemBlock MemoryPool::allocate(int size) { void MemoryPool::release(const MemBlock &mb) { pthread_mutex_lock(&gMPoolMutex); - BlockList *block = (BlockList *)mb.internalData; + auto *block = (BlockList *)mb.internalData; // just to be sure there's nothing important left in buffers.. VALGRIND_MAKE_MEM_UNDEFINED(block->data->data, block->size); diff --git a/encfs/RawFileIO.cpp b/encfs/RawFileIO.cpp index accb3fd..1eb1e93 100644 --- a/encfs/RawFileIO.cpp +++ b/encfs/RawFileIO.cpp @@ -23,9 +23,9 @@ #endif #include "internal/easylogging++.h" #include +#include #include #include -#include #include #include diff --git a/encfs/SSL_Cipher.cpp b/encfs/SSL_Cipher.cpp index 51e5ccf..a3ab05a 100644 --- a/encfs/SSL_Cipher.cpp +++ b/encfs/SSL_Cipher.cpp @@ -479,7 +479,7 @@ static uint64_t _checksum_64(SSLKey *key, const unsigned char *data, for (unsigned int i = 0; i < (mdLen - 1); ++i) h[i % 8] ^= (unsigned char)(md[i]); - uint64_t value = (uint64_t)h[0]; + auto value = (uint64_t)h[0]; for (int i = 1; i < 8; ++i) value = (value << 8) | (uint64_t)h[i]; return value; diff --git a/encfs/XmlReader.cpp b/encfs/XmlReader.cpp index 8016d9c..c4e938b 100644 --- a/encfs/XmlReader.cpp +++ b/encfs/XmlReader.cpp @@ -145,7 +145,7 @@ class XmlNode : virtual public XmlValue { if (name[0] == '@') { const char *value = element->Attribute(name + 1); if (value) - return XmlValuePtr(new XmlValue(value)); + return std::make_shared(value); else return XmlValuePtr(); } else { @@ -182,13 +182,13 @@ XmlValuePtr XmlReader::operator[](const char *name) const { tinyxml2::XMLNode *node = pd->doc->FirstChildElement(name); if (node == nullptr) { RLOG(ERROR) << "Xml node " << name << " not found"; - return XmlValuePtr(new XmlValue()); + return std::make_shared(); } tinyxml2::XMLElement *element = node->ToElement(); if (element == nullptr) { RLOG(ERROR) << "Xml node " << name << " not element"; - return XmlValuePtr(new XmlValue()); + return std::make_shared(); } return XmlValuePtr(new XmlNode(element)); diff --git a/encfs/autosprintf.cpp b/encfs/autosprintf.cpp index 7281825..fbb164b 100644 --- a/encfs/autosprintf.cpp +++ b/encfs/autosprintf.cpp @@ -27,10 +27,10 @@ /* Specification. */ #include "autosprintf.h" -#include // for va_list -#include // for NULL, vasprintf -#include // for free -#include // for strdup +#include // for va_list +#include // for NULL, vasprintf +#include // for free +#include // for strdup namespace gnu { @@ -54,7 +54,7 @@ autosprintf::~autosprintf() { free(str); } autosprintf::operator char *() const { if (str != nullptr) { size_t length = strlen(str) + 1; - char *copy = new char[length]; + auto *copy = new char[length]; memcpy(copy, str, length); return copy; } else diff --git a/encfs/base64.cpp b/encfs/base64.cpp index cc6eb19..9edf8ca 100644 --- a/encfs/base64.cpp +++ b/encfs/base64.cpp @@ -20,7 +20,7 @@ #include "base64.h" -#include // for toupper +#include // for toupper #include "Error.h" @@ -248,7 +248,7 @@ std::string B64StandardEncode(std::vector inputBuffer) { std::string encodedString; encodedString.reserve(B256ToB64Bytes(inputBuffer.size())); long temp; - std::vector::iterator cursor = inputBuffer.begin(); + auto cursor = inputBuffer.begin(); for (size_t idx = 0; idx < inputBuffer.size() / 3; idx++) { temp = (*cursor++) << 16; // Convert to big endian temp += (*cursor++) << 8; diff --git a/encfs/encfs.cpp b/encfs/encfs.cpp index 855863f..07d1451 100644 --- a/encfs/encfs.cpp +++ b/encfs/encfs.cpp @@ -18,17 +18,17 @@ #include "encfs.h" #include +#include #include +#include #include #include +#include #include -#include #include -#include #include #include #include -#include #include #include #ifdef linux diff --git a/encfs/main.cpp b/encfs/main.cpp index 3b10884..640fe4f 100644 --- a/encfs/main.cpp +++ b/encfs/main.cpp @@ -18,18 +18,18 @@ #include #include +#include #include +#include #include #include #include #include #include #include -#include #include #include #include -#include #include #include "Context.h" @@ -493,7 +493,7 @@ static bool processArgs(int argc, char *argv[], static void *idleMonitor(void *); void *encfs_init(fuse_conn_info *conn) { - EncFS_Context *ctx = (EncFS_Context *)fuse_get_context()->private_data; + auto *ctx = (EncFS_Context *)fuse_get_context()->private_data; // set fuse connection options conn->async_read = true; @@ -599,7 +599,7 @@ int main(int argc, char *argv[]) { // context is not a smart pointer because it will live for the life of // the filesystem. - auto ctx = std::shared_ptr(new EncFS_Context); + auto ctx = std::make_shared(); ctx->publicFilesystem = encfsArgs->opts->ownerCreate; RootPtr rootInfo = initFS(ctx.get(), encfsArgs->opts); @@ -709,7 +709,7 @@ const int ActivityCheckInterval = 10; static bool unmountFS(EncFS_Context *ctx); static void *idleMonitor(void *_arg) { - EncFS_Context *ctx = (EncFS_Context *)_arg; + auto *ctx = (EncFS_Context *)_arg; std::shared_ptr arg = ctx->args; const int timeoutCycles = 60 * arg->idleTimeout / ActivityCheckInterval; diff --git a/encfs/openssl.cpp b/encfs/openssl.cpp index d859f1c..f336a6b 100644 --- a/encfs/openssl.cpp +++ b/encfs/openssl.cpp @@ -20,9 +20,9 @@ #include "openssl.h" +#include #include #include -#include #define NO_DES #include From 169101c80b78084e089edb7f33f5f90a70ae5b0d Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Thu, 3 Aug 2017 22:10:25 -0700 Subject: [PATCH 03/10] modernize: use bool literals, raw string literal --- encfs/FileUtils.cpp | 5 +++-- encfs/NameIO.h | 2 +- encfs/XmlReader.cpp | 2 +- encfs/main.cpp | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/encfs/FileUtils.cpp b/encfs/FileUtils.cpp index a8c5b38..2c4a8f3 100644 --- a/encfs/FileUtils.cpp +++ b/encfs/FileUtils.cpp @@ -22,7 +22,8 @@ #ifdef linux #define _XOPEN_SOURCE 500 // make sure pwrite() is pulled in #endif -#define _BSD_SOURCE // pick up setenv on RH7.3 +#define _BSD_SOURCE // pick up setenv on RH7.3 +#define _DEFAULT_SOURCE // Replaces _BSD_SOURCE #include "internal/easylogging++.h" #include @@ -1416,7 +1417,7 @@ std::string readPassword(int FD) { char buffer[1024]; string result; - while (1) { + while (true) { ssize_t rdSize = recv(FD, buffer, sizeof(buffer), 0); if (rdSize > 0) { diff --git a/encfs/NameIO.h b/encfs/NameIO.h index 2264a64..7085970 100644 --- a/encfs/NameIO.h +++ b/encfs/NameIO.h @@ -139,7 +139,7 @@ class NameIO { delete[] Name; \ Name = Name##_Raw; \ } \ - } while (0); + } while (false); } // namespace encfs diff --git a/encfs/XmlReader.cpp b/encfs/XmlReader.cpp index c4e938b..4685f6d 100644 --- a/encfs/XmlReader.cpp +++ b/encfs/XmlReader.cpp @@ -101,7 +101,7 @@ bool XmlValue::readB64(const char *path, unsigned char *data, return false; } if (!B64StandardDecode(data, (unsigned char *)s.data(), s.size())) { - RLOG(ERROR) << "B64 decode failure on \"" << s << "\""; + RLOG(ERROR) << R"(B64 decode failure on ")" << s << R"(")"; return false; } diff --git a/encfs/main.cpp b/encfs/main.cpp index 640fe4f..104ba38 100644 --- a/encfs/main.cpp +++ b/encfs/main.cpp @@ -169,7 +169,7 @@ static void FuseUsage() { do { \ rAssert(out->fuseArgc < MaxFuseArgs); \ out->fuseArgv[out->fuseArgc++] = (ARG); \ - } while (0) + } while (false) static string slashTerminate(const string &src) { string result = src; @@ -233,7 +233,7 @@ static bool processArgs(int argc, char *argv[], {"require-macs", 0, nullptr, LONG_OPT_REQUIRE_MAC}, // require MACs {nullptr, 0, nullptr, 0}}; - while (1) { + while (true) { int option_index = 0; // 's' : single-threaded mode From 71e2bcc84daa424a910d5495788f8c1201afcef6 Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Thu, 3 Aug 2017 22:18:25 -0700 Subject: [PATCH 04/10] modernize: =default, pass by value --- encfs/BlockNameIO.cpp | 12 ++++++------ encfs/BlockNameIO.h | 4 ++-- encfs/Cipher.cpp | 4 ++-- encfs/CipherFileIO.cpp | 7 ++++--- encfs/CipherFileIO.h | 2 +- encfs/CipherKey.cpp | 4 ++-- encfs/ConfigReader.cpp | 4 ++-- encfs/Context.h | 9 ++++----- encfs/DirNode.cpp | 27 ++++++++++----------------- encfs/DirNode.h | 4 ++-- encfs/FileIO.cpp | 4 ++-- encfs/FileNode.h | 2 +- encfs/FileUtils.cpp | 4 ++-- encfs/Interface.cpp | 24 ++++++++++-------------- encfs/Interface.h | 2 +- encfs/MACFileIO.cpp | 8 ++++---- encfs/MACFileIO.h | 2 +- encfs/NameIO.cpp | 2 +- encfs/NullCipher.cpp | 14 +++++++------- encfs/NullNameIO.cpp | 4 ++-- encfs/RawFileIO.cpp | 5 +++-- encfs/RawFileIO.h | 2 +- encfs/SSL_Cipher.cpp | 2 +- encfs/SSL_Compat.h | 6 ++---- encfs/StreamNameIO.cpp | 10 ++++++---- encfs/StreamNameIO.h | 4 ++-- encfs/XmlReader.cpp | 6 +++--- 27 files changed, 84 insertions(+), 94 deletions(-) diff --git a/encfs/BlockNameIO.cpp b/encfs/BlockNameIO.cpp index 0fdff9a..89f96c2 100644 --- a/encfs/BlockNameIO.cpp +++ b/encfs/BlockNameIO.cpp @@ -22,6 +22,7 @@ #include #include +#include #include "Cipher.h" #include "CipherKey.h" @@ -94,20 +95,19 @@ Interface BlockNameIO::CurrentInterface(bool caseInsensitive) { return Interface("nameio/block", 4, 0, 2); } -BlockNameIO::BlockNameIO(const Interface &iface, - const std::shared_ptr &cipher, - const CipherKey &key, int blockSize, +BlockNameIO::BlockNameIO(const Interface &iface, std::shared_ptr cipher, + CipherKey key, int blockSize, bool caseInsensitiveEncoding) : _interface(iface.current()), _bs(blockSize), - _cipher(cipher), - _key(key), + _cipher(std::move(cipher)), + _key(std::move(key)), _caseInsensitive(caseInsensitiveEncoding) { // just to be safe.. rAssert(blockSize < 128); } -BlockNameIO::~BlockNameIO() {} +BlockNameIO::~BlockNameIO() = default; Interface BlockNameIO::interface() const { return CurrentInterface(_caseInsensitive); diff --git a/encfs/BlockNameIO.h b/encfs/BlockNameIO.h index 4ea0594..653159f 100644 --- a/encfs/BlockNameIO.h +++ b/encfs/BlockNameIO.h @@ -41,8 +41,8 @@ class BlockNameIO : public NameIO { public: static Interface CurrentInterface(bool caseInsensitive = false); - BlockNameIO(const Interface &iface, const std::shared_ptr &cipher, - const CipherKey &key, int blockSize, + BlockNameIO(const Interface &iface, std::shared_ptr cipher, + CipherKey key, int blockSize, bool caseInsensitiveEncoding = false); virtual ~BlockNameIO(); diff --git a/encfs/Cipher.cpp b/encfs/Cipher.cpp index 00b9a8d..50422d9 100644 --- a/encfs/Cipher.cpp +++ b/encfs/Cipher.cpp @@ -148,9 +148,9 @@ std::shared_ptr Cipher::New(const Interface &iface, int keyLen) { return result; } -Cipher::Cipher() {} +Cipher::Cipher() = default; -Cipher::~Cipher() {} +Cipher::~Cipher() = default; unsigned int Cipher::MAC_32(const unsigned char *src, int len, const CipherKey &key, uint64_t *chainedIV) const { diff --git a/encfs/CipherFileIO.cpp b/encfs/CipherFileIO.cpp index 40e3d5d..0a34efc 100644 --- a/encfs/CipherFileIO.cpp +++ b/encfs/CipherFileIO.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include "BlockFileIO.h" #include "Cipher.h" @@ -47,10 +48,10 @@ static Interface CipherFileIO_iface("FileIO/Cipher", 2, 0, 1); const int HEADER_SIZE = 8; // 64 bit initialization vector.. -CipherFileIO::CipherFileIO(const std::shared_ptr &_base, +CipherFileIO::CipherFileIO(std::shared_ptr _base, const FSConfigPtr &cfg) : BlockFileIO(cfg->config->blockSize, cfg), - base(_base), + base(std::move(_base)), haveHeader(cfg->config->uniqueIV), externalIV(0), fileIV(0), @@ -63,7 +64,7 @@ CipherFileIO::CipherFileIO(const std::shared_ptr &_base, << "FS block size must be multiple of cipher block size"; } -CipherFileIO::~CipherFileIO() {} +CipherFileIO::~CipherFileIO() = default; Interface CipherFileIO::interface() const { return CipherFileIO_iface; } diff --git a/encfs/CipherFileIO.h b/encfs/CipherFileIO.h index a1bfd84..de88d85 100644 --- a/encfs/CipherFileIO.h +++ b/encfs/CipherFileIO.h @@ -45,7 +45,7 @@ struct IORequest; */ class CipherFileIO : public BlockFileIO { public: - CipherFileIO(const std::shared_ptr &base, const FSConfigPtr &cfg); + CipherFileIO(std::shared_ptr base, const FSConfigPtr &cfg); virtual ~CipherFileIO(); virtual Interface interface() const; diff --git a/encfs/CipherKey.cpp b/encfs/CipherKey.cpp index bf3f168..5a914e6 100644 --- a/encfs/CipherKey.cpp +++ b/encfs/CipherKey.cpp @@ -22,8 +22,8 @@ namespace encfs { -AbstractCipherKey::AbstractCipherKey() {} +AbstractCipherKey::AbstractCipherKey() = default; -AbstractCipherKey::~AbstractCipherKey() {} +AbstractCipherKey::~AbstractCipherKey() = default; } // namespace encfs diff --git a/encfs/ConfigReader.cpp b/encfs/ConfigReader.cpp index d3f52d1..9ad2912 100644 --- a/encfs/ConfigReader.cpp +++ b/encfs/ConfigReader.cpp @@ -33,9 +33,9 @@ using namespace std; namespace encfs { -ConfigReader::ConfigReader() {} +ConfigReader::ConfigReader() = default; -ConfigReader::~ConfigReader() {} +ConfigReader::~ConfigReader() = default; // read the entire file into a ConfigVar instance and then use that to decode // into mapped variables. diff --git a/encfs/Context.h b/encfs/Context.h index 201da47..8d62fcf 100644 --- a/encfs/Context.h +++ b/encfs/Context.h @@ -21,14 +21,14 @@ #ifndef _Context_incl_ #define _Context_incl_ -#include #include +#include +#include #include #include #include #include #include -#include #include "encfs.h" @@ -47,7 +47,7 @@ class EncFS_Context { std::shared_ptr lookupNode(const char *path); void getAndResetUsageCounter(int *usage, int *openCount); - + void putNode(const char *path, std::shared_ptr node); void eraseNode(const char *path, std::shared_ptr fnode); @@ -85,8 +85,7 @@ class EncFS_Context { * us. */ - typedef std::unordered_map>> + typedef std::unordered_map>> FileMap; mutable pthread_mutex_t contextMutex; diff --git a/encfs/DirNode.cpp b/encfs/DirNode.cpp index f3d32e6..2a39ef2 100644 --- a/encfs/DirNode.cpp +++ b/encfs/DirNode.cpp @@ -37,6 +37,7 @@ #include "internal/easylogging++.h" #include +#include #include "Context.h" #include "Error.h" @@ -51,20 +52,13 @@ class DirDeleter { void operator()(DIR *d) { ::closedir(d); } }; -DirTraverse::DirTraverse(const std::shared_ptr &_dirPtr, uint64_t _iv, - const std::shared_ptr &_naming) - : dir(_dirPtr), iv(_iv), naming(_naming) {} +DirTraverse::DirTraverse(std::shared_ptr _dirPtr, uint64_t _iv, + std::shared_ptr _naming) + : dir(std::move(_dirPtr)), iv(_iv), naming(std::move(_naming)) {} -DirTraverse::DirTraverse(const DirTraverse &src) - : dir(src.dir), iv(src.iv), naming(src.naming) {} +DirTraverse::DirTraverse(const DirTraverse &src) = default; -DirTraverse &DirTraverse::operator=(const DirTraverse &src) { - dir = src.dir; - iv = src.iv; - naming = src.naming; - - return *this; -} +DirTraverse &DirTraverse::operator=(const DirTraverse &src) = default; DirTraverse::~DirTraverse() { dir.reset(); @@ -143,13 +137,12 @@ class RenameOp { list::const_iterator last; public: - RenameOp(DirNode *_dn, const std::shared_ptr > &_renameList) - : dn(_dn), renameList(_renameList) { + RenameOp(DirNode *_dn, std::shared_ptr > _renameList) + : dn(_dn), renameList(std::move(_renameList)) { last = renameList->begin(); } - RenameOp(const RenameOp &src) - : dn(src.dn), renameList(src.renameList), last(src.last) {} + RenameOp(const RenameOp &src) = default; ~RenameOp(); @@ -252,7 +245,7 @@ DirNode::DirNode(EncFS_Context *_ctx, const string &sourceDir, naming = fsConfig->nameCoding; } -DirNode::~DirNode() {} +DirNode::~DirNode() = default; bool DirNode::hasDirectoryNameDependency() const { return naming ? naming->getChainedNameIV() : false; diff --git a/encfs/DirNode.h b/encfs/DirNode.h index a88082d..027fbea 100644 --- a/encfs/DirNode.h +++ b/encfs/DirNode.h @@ -48,8 +48,8 @@ struct RenameEl; class DirTraverse { public: - DirTraverse(const std::shared_ptr &dirPtr, uint64_t iv, - const std::shared_ptr &naming); + DirTraverse(std::shared_ptr dirPtr, uint64_t iv, + std::shared_ptr naming); DirTraverse(const DirTraverse &src); ~DirTraverse(); diff --git a/encfs/FileIO.cpp b/encfs/FileIO.cpp index ab9b363..2f653e0 100644 --- a/encfs/FileIO.cpp +++ b/encfs/FileIO.cpp @@ -22,9 +22,9 @@ namespace encfs { -FileIO::FileIO() {} +FileIO::FileIO() = default; -FileIO::~FileIO() {} +FileIO::~FileIO() = default; int FileIO::blockSize() const { return 1; } diff --git a/encfs/FileNode.h b/encfs/FileNode.h index b522200..22accf4 100644 --- a/encfs/FileNode.h +++ b/encfs/FileNode.h @@ -21,13 +21,13 @@ #ifndef _FileNode_incl_ #define _FileNode_incl_ +#include #include #include #include #include #include #include -#include #include "CipherKey.h" #include "FSConfig.h" diff --git a/encfs/FileUtils.cpp b/encfs/FileUtils.cpp index 2c4a8f3..23c2e9a 100644 --- a/encfs/FileUtils.cpp +++ b/encfs/FileUtils.cpp @@ -119,9 +119,9 @@ struct ConfigInfo { {".encfs", Config_Prehistoric, nullptr, nullptr, nullptr, 0, 0}, {nullptr, Config_None, nullptr, nullptr, nullptr, 0, 0}}; -EncFS_Root::EncFS_Root() {} +EncFS_Root::EncFS_Root() = default; -EncFS_Root::~EncFS_Root() {} +EncFS_Root::~EncFS_Root() = default; bool fileExists(const char *fileName) { struct stat buf; diff --git a/encfs/Interface.cpp b/encfs/Interface.cpp index 8cd8def..18fb6e0 100644 --- a/encfs/Interface.cpp +++ b/encfs/Interface.cpp @@ -20,6 +20,8 @@ #include "Interface.h" +#include + #include "ConfigVar.h" #include "Error.h" @@ -28,25 +30,19 @@ namespace encfs { Interface::Interface(const char *name_, int Current, int Revision, int Age) : _name(name_), _current(Current), _revision(Revision), _age(Age) {} -Interface::Interface(const std::string &name_, int Current, int Revision, - int Age) - : _name(name_), _current(Current), _revision(Revision), _age(Age) {} +Interface::Interface(std::string name_, int Current, int Revision, int Age) + : _name(std::move(name_)), + _current(Current), + _revision(Revision), + _age(Age) {} Interface::Interface(const Interface &src) - : _name(src._name), - _current(src._current), - _revision(src._revision), - _age(src._age) {} + + = default; Interface::Interface() : _current(0), _revision(0), _age(0) {} -Interface &Interface::operator=(const Interface &src) { - _name = src._name; - _current = src._current; - _revision = src._revision; - _age = src._age; - return *this; -} +Interface &Interface::operator=(const Interface &src) = default; const std::string &Interface::name() const { return _name; } diff --git a/encfs/Interface.h b/encfs/Interface.h index 2ec101a..d1cffba 100644 --- a/encfs/Interface.h +++ b/encfs/Interface.h @@ -37,7 +37,7 @@ class Interface { are implemented. */ Interface(const char *name, int Current, int Revision, int Age); - Interface(const std::string &name, int Current, int Revision, int Age); + Interface(std::string name, int Current, int Revision, int Age); Interface(const Interface &src); Interface(); diff --git a/encfs/MACFileIO.cpp b/encfs/MACFileIO.cpp index 6d2897f..e03a618 100644 --- a/encfs/MACFileIO.cpp +++ b/encfs/MACFileIO.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "BlockFileIO.h" #include "Cipher.h" @@ -56,10 +57,9 @@ int dataBlockSize(const FSConfigPtr &cfg) { cfg->config->blockMACRandBytes; } -MACFileIO::MACFileIO(const std::shared_ptr &_base, - const FSConfigPtr &cfg) +MACFileIO::MACFileIO(std::shared_ptr _base, const FSConfigPtr &cfg) : BlockFileIO(dataBlockSize(cfg), cfg), - base(_base), + base(std::move(_base)), cipher(cfg->cipher), key(cfg->key), macBytes(cfg->config->blockMACBytes), @@ -72,7 +72,7 @@ MACFileIO::MACFileIO(const std::shared_ptr &_base, << ", randBytes = " << cfg->config->blockMACRandBytes; } -MACFileIO::~MACFileIO() {} +MACFileIO::~MACFileIO() = default; Interface MACFileIO::interface() const { return MACFileIO_iface; } diff --git a/encfs/MACFileIO.h b/encfs/MACFileIO.h index 6bf4a25..fe39bf7 100644 --- a/encfs/MACFileIO.h +++ b/encfs/MACFileIO.h @@ -44,7 +44,7 @@ class MACFileIO : public BlockFileIO { result in a warning message from encfs -- the garbled data will still be made available.. */ - MACFileIO(const std::shared_ptr &base, const FSConfigPtr &cfg); + MACFileIO(std::shared_ptr base, const FSConfigPtr &cfg); MACFileIO(); virtual ~MACFileIO(); diff --git a/encfs/NameIO.cpp b/encfs/NameIO.cpp index 7dbf9dc..7a3ea52 100644 --- a/encfs/NameIO.cpp +++ b/encfs/NameIO.cpp @@ -127,7 +127,7 @@ std::shared_ptr NameIO::New(const Interface &iface, NameIO::NameIO() : chainedNameIV(false), reverseEncryption(false) {} -NameIO::~NameIO() {} +NameIO::~NameIO() = default; void NameIO::setChainedNameIV(bool enable) { chainedNameIV = enable; } diff --git a/encfs/NullCipher.cpp b/encfs/NullCipher.cpp index 4c787d6..4a6e05b 100644 --- a/encfs/NullCipher.cpp +++ b/encfs/NullCipher.cpp @@ -49,24 +49,24 @@ static bool NullCipher_registered = Cipher::Register( class NullKey : public AbstractCipherKey { public: - NullKey() {} - virtual ~NullKey() {} + NullKey() = default; + virtual ~NullKey() = default; }; class NullDestructor { public: - NullDestructor() {} - NullDestructor(const NullDestructor &) {} - ~NullDestructor() {} + NullDestructor() = default; + NullDestructor(const NullDestructor &) = default; + ~NullDestructor() = default; - NullDestructor &operator=(const NullDestructor &) { return *this; } + NullDestructor &operator=(const NullDestructor &) = default; void operator()(NullKey *&) {} }; std::shared_ptr gNullKey(new NullKey(), NullDestructor()); NullCipher::NullCipher(const Interface &iface_) { this->iface = iface_; } -NullCipher::~NullCipher() {} +NullCipher::~NullCipher() = default; Interface NullCipher::interface() const { return iface; } diff --git a/encfs/NullNameIO.cpp b/encfs/NullNameIO.cpp index a2f6e34..26b0251 100644 --- a/encfs/NullNameIO.cpp +++ b/encfs/NullNameIO.cpp @@ -41,9 +41,9 @@ static Interface NNIOIface("nameio/null", 1, 0, 0); static bool NullNameIO_registered = NameIO::Register("Null", "No encryption of filenames", NNIOIface, NewNNIO); -NullNameIO::NullNameIO() {} +NullNameIO::NullNameIO() = default; -NullNameIO::~NullNameIO() {} +NullNameIO::~NullNameIO() = default; Interface NullNameIO::interface() const { return NNIOIface; } diff --git a/encfs/RawFileIO.cpp b/encfs/RawFileIO.cpp index 1eb1e93..fdfaccd 100644 --- a/encfs/RawFileIO.cpp +++ b/encfs/RawFileIO.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include "Error.h" #include "FileIO.h" @@ -53,8 +54,8 @@ inline void swap(int &x, int &y) { RawFileIO::RawFileIO() : knownSize(false), fileSize(0), fd(-1), oldfd(-1), canWrite(false) {} -RawFileIO::RawFileIO(const std::string &fileName) - : name(fileName), +RawFileIO::RawFileIO(std::string fileName) + : name(std::move(fileName)), knownSize(false), fileSize(0), fd(-1), diff --git a/encfs/RawFileIO.h b/encfs/RawFileIO.h index 1214a56..bd74fe6 100644 --- a/encfs/RawFileIO.h +++ b/encfs/RawFileIO.h @@ -32,7 +32,7 @@ namespace encfs { class RawFileIO : public FileIO { public: RawFileIO(); - RawFileIO(const std::string &fileName); + RawFileIO(std::string fileName); virtual ~RawFileIO(); virtual Interface interface() const; diff --git a/encfs/SSL_Cipher.cpp b/encfs/SSL_Cipher.cpp index a3ab05a..d0fb9e4 100644 --- a/encfs/SSL_Cipher.cpp +++ b/encfs/SSL_Cipher.cpp @@ -342,7 +342,7 @@ SSL_Cipher::SSL_Cipher(const Interface &iface_, const Interface &realIface_, } } -SSL_Cipher::~SSL_Cipher() {} +SSL_Cipher::~SSL_Cipher() = default; Interface SSL_Cipher::interface() const { return realIface; } diff --git a/encfs/SSL_Compat.h b/encfs/SSL_Compat.h index f7b1629..276bf26 100644 --- a/encfs/SSL_Compat.h +++ b/encfs/SSL_Compat.h @@ -30,8 +30,7 @@ #define HMAC_CTX_reset HMAC_CTX_cleanup // Missing methods (based on 1.1.0 versions) -HMAC_CTX *HMAC_CTX_new(void) -{ +HMAC_CTX *HMAC_CTX_new(void) { HMAC_CTX *ctx = (HMAC_CTX *)OPENSSL_malloc(sizeof(HMAC_CTX)); if (ctx != NULL) { memset(ctx, 0, sizeof(HMAC_CTX)); @@ -40,8 +39,7 @@ HMAC_CTX *HMAC_CTX_new(void) return ctx; } -void HMAC_CTX_free(HMAC_CTX *ctx) -{ +void HMAC_CTX_free(HMAC_CTX *ctx) { if (ctx != NULL) { HMAC_CTX_cleanup(ctx); OPENSSL_free(ctx); diff --git a/encfs/StreamNameIO.cpp b/encfs/StreamNameIO.cpp index e07c4e5..ec06852 100644 --- a/encfs/StreamNameIO.cpp +++ b/encfs/StreamNameIO.cpp @@ -22,6 +22,7 @@ #include "internal/easylogging++.h" #include +#include #include "Cipher.h" #include "CipherKey.h" @@ -72,11 +73,12 @@ Interface StreamNameIO::CurrentInterface() { } StreamNameIO::StreamNameIO(const Interface &iface, - const std::shared_ptr &cipher, - const CipherKey &key) - : _interface(iface.current()), _cipher(cipher), _key(key) {} + std::shared_ptr cipher, CipherKey key) + : _interface(iface.current()), + _cipher(std::move(cipher)), + _key(std::move(key)) {} -StreamNameIO::~StreamNameIO() {} +StreamNameIO::~StreamNameIO() = default; Interface StreamNameIO::interface() const { return CurrentInterface(); } diff --git a/encfs/StreamNameIO.h b/encfs/StreamNameIO.h index 5bb3134..43a417e 100644 --- a/encfs/StreamNameIO.h +++ b/encfs/StreamNameIO.h @@ -36,8 +36,8 @@ class StreamNameIO : public NameIO { public: static Interface CurrentInterface(); - StreamNameIO(const Interface &iface, const std::shared_ptr &cipher, - const CipherKey &key); + StreamNameIO(const Interface &iface, std::shared_ptr cipher, + CipherKey key); virtual ~StreamNameIO(); virtual Interface interface() const; diff --git a/encfs/XmlReader.cpp b/encfs/XmlReader.cpp index 4685f6d..33a343a 100644 --- a/encfs/XmlReader.cpp +++ b/encfs/XmlReader.cpp @@ -34,7 +34,7 @@ namespace encfs { -XmlValue::~XmlValue() {} +XmlValue::~XmlValue() = default; XmlValuePtr XmlValue::operator[](const char *path) const { return find(path); } @@ -139,7 +139,7 @@ class XmlNode : virtual public XmlValue { XmlNode(const tinyxml2::XMLElement *element_) : XmlValue(safeValueForNode(element_)), element(element_) {} - virtual ~XmlNode() {} + virtual ~XmlNode() = default; virtual XmlValuePtr find(const char *name) const { if (name[0] == '@') { @@ -164,7 +164,7 @@ struct XmlReader::XmlReaderData { XmlReader::XmlReader() : pd(new XmlReaderData()) {} -XmlReader::~XmlReader() {} +XmlReader::~XmlReader() = default; bool XmlReader::load(const char *fileName) { pd->doc.reset(new tinyxml2::XMLDocument()); From 7a4e0c41dbff768388e8a04569b8f61404907143 Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Thu, 3 Aug 2017 22:27:18 -0700 Subject: [PATCH 05/10] modernize: use override, using aliases --- encfs/Cipher.cpp | 2 +- encfs/Cipher.h | 6 +++--- encfs/CipherKey.h | 2 +- encfs/Context.h | 4 ++-- encfs/FSConfig.h | 2 +- encfs/FileUtils.h | 2 +- encfs/NameIO.cpp | 2 +- encfs/NameIO.h | 4 ++-- encfs/NullCipher.cpp | 2 +- encfs/SSL_Cipher.cpp | 2 +- encfs/SSL_Cipher.h | 2 +- encfs/XmlReader.cpp | 4 ++-- encfs/XmlReader.h | 2 +- 13 files changed, 18 insertions(+), 18 deletions(-) diff --git a/encfs/Cipher.cpp b/encfs/Cipher.cpp index 50422d9..cb4a2c2 100644 --- a/encfs/Cipher.cpp +++ b/encfs/Cipher.cpp @@ -56,7 +56,7 @@ struct CipherAlg { Range blockSize; }; -typedef multimap CipherMap_t; +using CipherMap_t = multimap; static CipherMap_t *gCipherMap = nullptr; std::list Cipher::GetAlgorithmList( diff --git a/encfs/Cipher.h b/encfs/Cipher.h index 89ed4ec..5c5d709 100644 --- a/encfs/Cipher.h +++ b/encfs/Cipher.h @@ -44,8 +44,8 @@ class Cipher { public: // if no key length was indicated when cipher was registered, then keyLen // <= 0 will be used. - typedef std::shared_ptr (*CipherConstructor)(const Interface &iface, - int keyLenBits); + using CipherConstructor = std::shared_ptr (*)(const Interface &iface, + int keyLenBits); struct CipherAlgorithm { std::string name; @@ -55,7 +55,7 @@ class Cipher { Range blockSize; }; - typedef std::list AlgorithmList; + using AlgorithmList = std::list; static AlgorithmList GetAlgorithmList(bool includeHidden = false); static std::shared_ptr New(const Interface &iface, int keyLen = -1); diff --git a/encfs/CipherKey.h b/encfs/CipherKey.h index 42ddb8a..505b899 100644 --- a/encfs/CipherKey.h +++ b/encfs/CipherKey.h @@ -31,7 +31,7 @@ class AbstractCipherKey { virtual ~AbstractCipherKey(); }; -typedef std::shared_ptr CipherKey; +using CipherKey = std::shared_ptr; } // namespace encfs diff --git a/encfs/Context.h b/encfs/Context.h index 8d62fcf..56b0fc4 100644 --- a/encfs/Context.h +++ b/encfs/Context.h @@ -85,8 +85,8 @@ class EncFS_Context { * us. */ - typedef std::unordered_map>> - FileMap; + using FileMap = + std::unordered_map>>; mutable pthread_mutex_t contextMutex; FileMap openFiles; diff --git a/encfs/FSConfig.h b/encfs/FSConfig.h index 39e2859..26fb0d0 100644 --- a/encfs/FSConfig.h +++ b/encfs/FSConfig.h @@ -130,7 +130,7 @@ struct FSConfig { : forceDecode(false), reverseEncryption(false), idleTracking(false) {} }; -typedef std::shared_ptr FSConfigPtr; +using FSConfigPtr = std::shared_ptr; } // namespace encfs diff --git a/encfs/FileUtils.h b/encfs/FileUtils.h index 7efbbf0..3e42092 100644 --- a/encfs/FileUtils.h +++ b/encfs/FileUtils.h @@ -60,7 +60,7 @@ struct EncFS_Root { ~EncFS_Root(); }; -typedef std::shared_ptr RootPtr; +using RootPtr = std::shared_ptr; enum ConfigMode { Config_Prompt, Config_Standard, Config_Paranoia }; diff --git a/encfs/NameIO.cpp b/encfs/NameIO.cpp index 7a3ea52..9cdb1c5 100644 --- a/encfs/NameIO.cpp +++ b/encfs/NameIO.cpp @@ -55,7 +55,7 @@ struct NameIOAlg { Interface iface; }; -typedef multimap NameIOMap_t; +using NameIOMap_t = multimap; static NameIOMap_t *gNameIOMap = nullptr; list NameIO::GetAlgorithmList(bool includeHidden) { diff --git a/encfs/NameIO.h b/encfs/NameIO.h index 7085970..3129dc5 100644 --- a/encfs/NameIO.h +++ b/encfs/NameIO.h @@ -37,7 +37,7 @@ class Cipher; class NameIO { public: - typedef std::shared_ptr (*Constructor)( + using Constructor = std::shared_ptr (*)( const Interface &iface, const std::shared_ptr &cipher, const CipherKey &key); @@ -47,7 +47,7 @@ class NameIO { Interface iface; }; - typedef std::list AlgorithmList; + using AlgorithmList = std::list; static AlgorithmList GetAlgorithmList(bool includeHidden = false); static std::shared_ptr New(const Interface &iface, diff --git a/encfs/NullCipher.cpp b/encfs/NullCipher.cpp index 4a6e05b..4e916ca 100644 --- a/encfs/NullCipher.cpp +++ b/encfs/NullCipher.cpp @@ -50,7 +50,7 @@ static bool NullCipher_registered = Cipher::Register( class NullKey : public AbstractCipherKey { public: NullKey() = default; - virtual ~NullKey() = default; + ~NullKey() override = default; }; class NullDestructor { diff --git a/encfs/SSL_Cipher.cpp b/encfs/SSL_Cipher.cpp index d0fb9e4..7c6c749 100644 --- a/encfs/SSL_Cipher.cpp +++ b/encfs/SSL_Cipher.cpp @@ -238,7 +238,7 @@ class SSLKey : public AbstractCipherKey { HMAC_CTX *mac_ctx; SSLKey(int keySize, int ivLength); - ~SSLKey(); + ~SSLKey() override; }; SSLKey::SSLKey(int keySize_, int ivLength_) { diff --git a/encfs/SSL_Cipher.h b/encfs/SSL_Cipher.h index f9613ff..007ed2e 100644 --- a/encfs/SSL_Cipher.h +++ b/encfs/SSL_Cipher.h @@ -31,7 +31,7 @@ #ifndef EVP_CIPHER struct evp_cipher_st; -typedef struct evp_cipher_st EVP_CIPHER; +using EVP_CIPHER = struct evp_cipher_st; #endif namespace encfs { diff --git a/encfs/XmlReader.cpp b/encfs/XmlReader.cpp index 33a343a..a3cd9a6 100644 --- a/encfs/XmlReader.cpp +++ b/encfs/XmlReader.cpp @@ -139,9 +139,9 @@ class XmlNode : virtual public XmlValue { XmlNode(const tinyxml2::XMLElement *element_) : XmlValue(safeValueForNode(element_)), element(element_) {} - virtual ~XmlNode() = default; + ~XmlNode() override = default; - virtual XmlValuePtr find(const char *name) const { + XmlValuePtr find(const char *name) const override { if (name[0] == '@') { const char *value = element->Attribute(name + 1); if (value) diff --git a/encfs/XmlReader.h b/encfs/XmlReader.h index 4a56b16..cf1d3e4 100644 --- a/encfs/XmlReader.h +++ b/encfs/XmlReader.h @@ -29,7 +29,7 @@ namespace encfs { class XmlValue; -typedef std::shared_ptr XmlValuePtr; +using XmlValuePtr = std::shared_ptr; class XmlValue { std::string value; From c37ab8e67196f0cac4d1835b80c3ccf1c9b94b93 Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Thu, 3 Aug 2017 22:42:39 -0700 Subject: [PATCH 06/10] modernize: explicit conversions, braces around blocks --- encfs/BlockFileIO.cpp | 45 +++++++++-------- encfs/BlockNameIO.cpp | 14 +++--- encfs/Cipher.cpp | 8 +-- encfs/CipherFileIO.cpp | 47 +++++++++++------- encfs/ConfigReader.cpp | 5 +- encfs/ConfigVar.cpp | 12 +++-- encfs/DirNode.cpp | 33 +++++++------ encfs/FileNode.cpp | 32 +++++++----- encfs/FileUtils.cpp | 103 +++++++++++++++++++++++---------------- encfs/Interface.cpp | 19 +++++--- encfs/MACFileIO.cpp | 9 ++-- encfs/MemoryPool.cpp | 9 ++-- encfs/NameIO.cpp | 15 +++--- encfs/RawFileIO.cpp | 11 +++-- encfs/SSL_Cipher.cpp | 47 ++++++++++-------- encfs/StreamNameIO.cpp | 4 +- encfs/XmlReader.cpp | 16 +++--- encfs/autosprintf.cpp | 7 +-- encfs/base64.cpp | 46 +++++++++-------- encfs/encfs.cpp | 26 ++++++---- encfs/main.cpp | 26 +++++----- encfs/openssl.cpp | 12 +++-- encfs/readpassphrase.cpp | 19 ++++---- 23 files changed, 327 insertions(+), 238 deletions(-) diff --git a/encfs/BlockFileIO.cpp b/encfs/BlockFileIO.cpp index 06a39ae..89c035e 100644 --- a/encfs/BlockFileIO.cpp +++ b/encfs/BlockFileIO.cpp @@ -67,8 +67,7 @@ ssize_t BlockFileIO::cacheReadOneBlock(const IORequest &req) const { * in the last block of a file, which may be smaller than the blocksize. * For reverse encryption, the cache must not be used at all, because * the lower file may have changed behind our back. */ - if ((_noCache == false) && (req.offset == _cache.offset) && - (_cache.dataLen != 0)) { + if ((!_noCache) && (req.offset == _cache.offset) && (_cache.dataLen != 0)) { // satisfy request from cache int len = req.dataLen; if (_cache.dataLen < len) len = _cache.dataLen; // Don't read past EOF @@ -86,8 +85,9 @@ ssize_t BlockFileIO::cacheReadOneBlock(const IORequest &req) const { if (result > 0) { _cache.offset = req.offset; _cache.dataLen = result; // the amount we really have - if (result > req.dataLen) + if (result > req.dataLen) { result = req.dataLen; // only as much as requested + } memcpy(req.data, _cache.data, result); } return result; @@ -134,15 +134,15 @@ ssize_t BlockFileIO::read(const IORequest &req) const { blockReq.data = nullptr; unsigned char *out = req.data; - while (size) { + while (size != 0u) { blockReq.offset = blockNum * _blockSize; // if we're reading a full block, then read directly into the // result buffer instead of using a temporary - if (partialOffset == 0 && size >= (size_t)_blockSize) + if (partialOffset == 0 && size >= (size_t)_blockSize) { blockReq.data = out; - else { - if (!mb.data) mb = MemoryPool::allocate(_blockSize); + } else { + if (mb.data == nullptr) mb = MemoryPool::allocate(_blockSize); blockReq.data = mb.data; } @@ -153,8 +153,9 @@ ssize_t BlockFileIO::read(const IORequest &req) const { CHECK(cpySize <= readSize); // if we read to a temporary buffer, then move the data - if (blockReq.data != out) + if (blockReq.data != out) { memcpy(out, blockReq.data + partialOffset, cpySize); + } result += cpySize; size -= cpySize; @@ -165,7 +166,7 @@ ssize_t BlockFileIO::read(const IORequest &req) const { if (readSize < _blockSize) break; } - if (mb.data) MemoryPool::release(mb); + if (mb.data != nullptr) MemoryPool::release(mb); } return result; @@ -202,8 +203,9 @@ bool BlockFileIO::write(const IORequest &req) { // if writing a partial block, but at least as much as what is // already there.. - if (blockNum == lastFileBlock && req.dataLen >= lastBlockSize) + if (blockNum == lastFileBlock && req.dataLen >= lastBlockSize) { return cacheWriteOneBlock(req); + } } // have to merge data with existing block(s).. @@ -216,7 +218,7 @@ bool BlockFileIO::write(const IORequest &req) { bool ok = true; size_t size = req.dataLen; unsigned char *inPtr = req.data; - while (size) { + while (size != 0u) { blockReq.offset = blockNum * _blockSize; int toCopy = min((size_t)(_blockSize - partialOffset), size); @@ -230,7 +232,7 @@ bool BlockFileIO::write(const IORequest &req) { } else { // need a temporary buffer, since we have to either merge or pad // the data. - if (!mb.data) mb = MemoryPool::allocate(_blockSize); + if (mb.data == nullptr) mb = MemoryPool::allocate(_blockSize); memset(mb.data, 0, _blockSize); blockReq.data = mb.data; @@ -243,8 +245,9 @@ bool BlockFileIO::write(const IORequest &req) { blockReq.dataLen = cacheReadOneBlock(blockReq); // extend data if necessary.. - if (partialOffset + toCopy > blockReq.dataLen) + if (partialOffset + toCopy > blockReq.dataLen) { blockReq.dataLen = partialOffset + toCopy; + } } // merge in the data to be written.. memcpy(blockReq.data + partialOffset, inPtr, toCopy); @@ -263,7 +266,7 @@ bool BlockFileIO::write(const IORequest &req) { partialOffset = 0; } - if (mb.data) MemoryPool::release(mb); + if (mb.data != nullptr) MemoryPool::release(mb); return ok; } @@ -290,7 +293,7 @@ void BlockFileIO::padFile(off_t oldSize, off_t newSize, bool forceWrite) { req.dataLen = oldSize % _blockSize; int outSize = newSize % _blockSize; // outSize > req.dataLen - if (outSize) { + if (outSize != 0) { memset(mb.data, 0, outSize); cacheReadOneBlock(req); req.dataLen = outSize; @@ -331,7 +334,7 @@ void BlockFileIO::padFile(off_t oldSize, off_t newSize, bool forceWrite) { } // 3. only necessary if write is forced and block is non 0 length - if (forceWrite && newBlockSize) { + if (forceWrite && (newBlockSize != 0)) { req.offset = newLastBlock * _blockSize; req.dataLen = newBlockSize; memset(mb.data, 0, req.dataLen); @@ -339,7 +342,7 @@ void BlockFileIO::padFile(off_t oldSize, off_t newSize, bool forceWrite) { } } - if (mb.data) MemoryPool::release(mb); + if (mb.data != nullptr) MemoryPool::release(mb); } int BlockFileIO::truncateBase(off_t size, FileIO *base) { @@ -353,13 +356,13 @@ int BlockFileIO::truncateBase(off_t size, FileIO *base) { // states that it will pad with 0's. // do the truncate so that the underlying filesystem can allocate // the space, and then we'll fill it in padFile.. - if (base) base->truncate(size); + if (base != nullptr) base->truncate(size); const bool forceWrite = true; padFile(oldSize, size, forceWrite); } else if (size == oldSize) { // the easiest case, but least likely.... - } else if (partialBlock) { + } else if (partialBlock != 0) { // partial block after truncate. Need to read in the block being // truncated before the truncate. Then write it back out afterwards, // since the encoding will change.. @@ -374,7 +377,7 @@ int BlockFileIO::truncateBase(off_t size, FileIO *base) { ssize_t rdSz = cacheReadOneBlock(req); // do the truncate - if (base) res = base->truncate(size); + if (base != nullptr) res = base->truncate(size); // write back out partial block req.dataLen = partialBlock; @@ -390,7 +393,7 @@ int BlockFileIO::truncateBase(off_t size, FileIO *base) { } else { // truncating on a block bounday. No need to re-encode the last // block.. - if (base) res = base->truncate(size); + if (base != nullptr) res = base->truncate(size); } return res; diff --git a/encfs/BlockNameIO.cpp b/encfs/BlockNameIO.cpp index 89f96c2..572ddf0 100644 --- a/encfs/BlockNameIO.cpp +++ b/encfs/BlockNameIO.cpp @@ -89,10 +89,11 @@ static bool BlockIO32_registered = NameIO::Register( */ Interface BlockNameIO::CurrentInterface(bool caseInsensitive) { // implement major version 4 plus support for two prior versions - if (caseInsensitive) + if (caseInsensitive) { return Interface("nameio/block32", 4, 0, 2); - else + } else { return Interface("nameio/block", 4, 0, 2); + } } BlockNameIO::BlockNameIO(const Interface &iface, std::shared_ptr cipher, @@ -118,10 +119,11 @@ int BlockNameIO::maxEncodedNameLen(int plaintextNameLen) const { // the size of too much space rather then too little. int numBlocks = (plaintextNameLen + _bs) / _bs; int encodedNameLen = numBlocks * _bs + 2; // 2 checksum bytes - if (_caseInsensitive) + if (_caseInsensitive) { return B256ToB32Bytes(encodedNameLen); - else + } else { return B256ToB64Bytes(encodedNameLen); + } } int BlockNameIO::maxDecodedNameLen(int encodedNameLen) const { @@ -145,7 +147,7 @@ int BlockNameIO::encodeName(const char *plaintextName, int length, uint64_t *iv, // store the IV before it is modified by the MAC call. uint64_t tmpIV = 0; - if (iv && _interface >= 3) tmpIV = *iv; + if ((iv != nullptr) && _interface >= 3) tmpIV = *iv; // include padding in MAC computation unsigned int mac = _cipher->MAC_16((unsigned char *)encodedName + 2, @@ -207,7 +209,7 @@ int BlockNameIO::decodeName(const char *encodedName, int length, uint64_t *iv, ((unsigned int)((unsigned char)tmpBuf[1])); uint64_t tmpIV = 0; - if (iv && _interface >= 3) tmpIV = *iv; + if ((iv != nullptr) && _interface >= 3) tmpIV = *iv; _cipher->blockDecode((unsigned char *)tmpBuf + 2, decodedStreamLen, (uint64_t)mac ^ tmpIV, _key); diff --git a/encfs/Cipher.cpp b/encfs/Cipher.cpp index cb4a2c2..1791459 100644 --- a/encfs/Cipher.cpp +++ b/encfs/Cipher.cpp @@ -65,7 +65,7 @@ std::list Cipher::GetAlgorithmList( list result; - if (!gCipherMap) return result; + if (gCipherMap == nullptr) return result; CipherMap_t::const_iterator it; CipherMap_t::const_iterator mapEnd = gCipherMap->end(); @@ -98,7 +98,7 @@ bool Cipher::Register(const char *name, const char *description, const Interface &iface, const Range &keyLength, const Range &blockSize, CipherConstructor fn, bool hidden) { - if (!gCipherMap) gCipherMap = new CipherMap_t; + if (gCipherMap == nullptr) gCipherMap = new CipherMap_t; CipherAlg ca; ca.hidden = hidden; @@ -114,7 +114,7 @@ bool Cipher::Register(const char *name, const char *description, std::shared_ptr Cipher::New(const string &name, int keyLen) { std::shared_ptr result; - if (gCipherMap) { + if (gCipherMap != nullptr) { CipherMap_t::const_iterator it = gCipherMap->find(name); if (it != gCipherMap->end()) { CipherConstructor fn = it->second.constructor; @@ -127,7 +127,7 @@ std::shared_ptr Cipher::New(const string &name, int keyLen) { } std::shared_ptr Cipher::New(const Interface &iface, int keyLen) { std::shared_ptr result; - if (gCipherMap) { + if (gCipherMap != nullptr) { CipherMap_t::const_iterator it; CipherMap_t::const_iterator mapEnd = gCipherMap->end(); diff --git a/encfs/CipherFileIO.cpp b/encfs/CipherFileIO.cpp index 0a34efc..17e109f 100644 --- a/encfs/CipherFileIO.cpp +++ b/encfs/CipherFileIO.cpp @@ -197,14 +197,16 @@ void CipherFileIO::initHeader() { unsigned char buf[8] = {0}; do { - if (!cipher->randomize(buf, 8, false)) + if (!cipher->randomize(buf, 8, false)) { throw Error("Unable to generate a random file IV"); + } fileIV = 0; for (int i = 0; i < 8; ++i) fileIV = (fileIV << 8) | (uint64_t)buf[i]; - if (fileIV == 0) + if (fileIV == 0) { RLOG(WARNING) << "Unexpected result: randomize returned 8 null bytes!"; + } } while (fileIV == 0); // don't accept 0 as an option.. if (base->isWritable()) { @@ -325,8 +327,9 @@ ssize_t CipherFileIO::readOneBlock(const IORequest &req) const { bool ok; if (readSize > 0) { - if (haveHeader && fileIV == 0) + if (haveHeader && fileIV == 0) { const_cast(this)->initHeader(); + } if (readSize != bs) { VLOG(1) << "streamRead(data, " << readSize << ", IV)"; @@ -372,8 +375,9 @@ bool CipherFileIO::writeOneBlock(const IORequest &req) { IORequest tmpReq = req; tmpReq.offset += HEADER_SIZE; ok = base->write(tmpReq); - } else + } else { ok = base->write(req); + } } else { VLOG(1) << "encodeBlock failed for block " << blockNum << ", size " << req.dataLen; @@ -385,43 +389,48 @@ bool CipherFileIO::writeOneBlock(const IORequest &req) { bool CipherFileIO::blockWrite(unsigned char *buf, int size, uint64_t _iv64) const { VLOG(1) << "Called blockWrite"; - if (!fsConfig->reverseEncryption) + if (!fsConfig->reverseEncryption) { return cipher->blockEncode(buf, size, _iv64, key); - else + } else { return cipher->blockDecode(buf, size, _iv64, key); + } } bool CipherFileIO::streamWrite(unsigned char *buf, int size, uint64_t _iv64) const { VLOG(1) << "Called streamWrite"; - if (!fsConfig->reverseEncryption) + if (!fsConfig->reverseEncryption) { return cipher->streamEncode(buf, size, _iv64, key); - else + } else { return cipher->streamDecode(buf, size, _iv64, key); + } } bool CipherFileIO::blockRead(unsigned char *buf, int size, uint64_t _iv64) const { - if (fsConfig->reverseEncryption) + if (fsConfig->reverseEncryption) { return cipher->blockEncode(buf, size, _iv64, key); - else { + } else { if (_allowHoles) { // special case - leave all 0's alone - for (int i = 0; i < size; ++i) + for (int i = 0; i < size; ++i) { if (buf[i] != 0) return cipher->blockDecode(buf, size, _iv64, key); + } return true; - } else + } else { return cipher->blockDecode(buf, size, _iv64, key); + } } } bool CipherFileIO::streamRead(unsigned char *buf, int size, uint64_t _iv64) const { - if (fsConfig->reverseEncryption) + if (fsConfig->reverseEncryption) { return cipher->streamEncode(buf, size, _iv64, key); - else + } else { return cipher->streamDecode(buf, size, _iv64, key); + } } int CipherFileIO::truncate(off_t size) { @@ -434,8 +443,9 @@ int CipherFileIO::truncate(off_t size) { if (!base->isWritable()) { // open for write.. int newFlags = lastFlags | O_RDWR; - if (base->open(newFlags) < 0) + if (base->open(newFlags) < 0) { VLOG(1) << "writeHeader failed to re-open for write"; + } } initHeader(); } @@ -483,8 +493,9 @@ ssize_t CipherFileIO::read(const IORequest &origReq) const { * to the data. */ if (req.offset < 0) { headerBytes = -req.offset; - if (req.dataLen < headerBytes) + if (req.dataLen < headerBytes) { headerBytes = req.dataLen; // only up to the number of bytes requested + } VLOG(1) << "Adding " << headerBytes << " header bytes"; // copy the header bytes into the data @@ -506,9 +517,9 @@ ssize_t CipherFileIO::read(const IORequest &origReq) const { // read the payload ssize_t readBytes = BlockFileIO::read(req); VLOG(1) << "read " << readBytes << " bytes from backing file"; - if (readBytes < 0) + if (readBytes < 0) { return readBytes; // Return error code - else { + } else { ssize_t sum = headerBytes + readBytes; VLOG(1) << "returning sum=" << sum; return sum; diff --git a/encfs/ConfigReader.cpp b/encfs/ConfigReader.cpp index 9ad2912..9a1fbab 100644 --- a/encfs/ConfigReader.cpp +++ b/encfs/ConfigReader.cpp @@ -127,10 +127,11 @@ ConfigVar ConfigReader::toVar() const { ConfigVar ConfigReader::operator[](const std::string &varName) const { // read only auto it = vars.find(varName); - if (it == vars.end()) + if (it == vars.end()) { return ConfigVar(); - else + } else { return it->second; + } } ConfigVar &ConfigReader::operator[](const std::string &varName) { diff --git a/encfs/ConfigVar.cpp b/encfs/ConfigVar.cpp index 27792f1..dfa5e94 100644 --- a/encfs/ConfigVar.cpp +++ b/encfs/ConfigVar.cpp @@ -43,10 +43,11 @@ ConfigVar::ConfigVar(const ConfigVar &src) { pd = src.pd; } ConfigVar::~ConfigVar() { pd.reset(); } ConfigVar &ConfigVar::operator=(const ConfigVar &src) { - if (src.pd == pd) + if (src.pd == pd) { return *this; - else + } else { pd = src.pd; + } return *this; } @@ -122,7 +123,7 @@ int ConfigVar::readInt() const { do { unsigned char tmp = buf[offset++]; - highBitSet = tmp & 0x80; + highBitSet = ((tmp & 0x80) != 0); value = (value << 7) | (int)(tmp & 0x7f); } while (highBitSet && offset < bytes); @@ -139,10 +140,11 @@ int ConfigVar::readInt(int defaultValue) const { int bytes = this->size(); int offset = at(); - if (offset >= bytes) + if (offset >= bytes) { return defaultValue; - else + } else { return readInt(); + } } bool ConfigVar::readBool(bool defaultValue) const { diff --git a/encfs/DirNode.cpp b/encfs/DirNode.cpp index 2a39ef2..35bbaf7 100644 --- a/encfs/DirNode.cpp +++ b/encfs/DirNode.cpp @@ -70,8 +70,8 @@ static bool _nextName(struct dirent *&de, const std::shared_ptr &dir, int *fileType, ino_t *inode) { de = ::readdir(dir.get()); - if (de) { - if (fileType) { + if (de != nullptr) { + if (fileType != nullptr) { #if defined(_DIRENT_HAVE_D_TYPE) || defined(__FreeBSD__) || defined(__APPLE__) *fileType = de->d_type; #else @@ -79,10 +79,10 @@ static bool _nextName(struct dirent *&de, const std::shared_ptr &dir, *fileType = 0; #endif } - if (inode) *inode = de->d_ino; + if (inode != nullptr) *inode = de->d_ino; return true; } else { - if (fileType) *fileType = 0; + if (fileType != nullptr) *fileType = 0; return false; } } @@ -477,8 +477,9 @@ std::shared_ptr DirNode::newRenameOp(const char *fromP, if (!genRenameList(*renameList.get(), fromP, toP)) { RLOG(WARNING) << "Error during generation of recursive rename list"; return std::shared_ptr(); - } else + } else { return std::make_shared(this, renameList); + } } int DirNode::mkdir(const char *plaintextPath, mode_t mode, uid_t uid, @@ -504,8 +505,9 @@ int DirNode::mkdir(const char *plaintextPath, mode_t mode, uid_t uid, RLOG(WARNING) << "mkdir error on " << cyName << " mode " << mode << ": " << strerror(eno); res = -eno; - } else + } else { res = 0; + } return res; } @@ -586,10 +588,11 @@ int DirNode::link(const char *from, const char *to) { VLOG(1) << "hard links not supported with external IV chaining!"; } else { res = ::link(fromCName.c_str(), toCName.c_str()); - if (res == -1) + if (res == -1) { res = -errno; - else + } else { res = 0; + } } return res; @@ -616,7 +619,7 @@ std::shared_ptr DirNode::renameNode(const char *from, const char *to, << cname; if (node->setName(to, cname.c_str(), newIV, forwardMode)) { - if (ctx) ctx->renameNode(from, to); + if (ctx != nullptr) ctx->renameNode(from, to); } else { // rename error! - put it back RLOG(ERROR) << "renameNode failed"; @@ -633,7 +636,7 @@ std::shared_ptr DirNode::findOrCreate(const char *plainName) { std::shared_ptr node; // See if we already have a FileNode for this path. - if (ctx) node = ctx->lookupNode(plainName); + if (ctx != nullptr) node = ctx->lookupNode(plainName); // If we don't, create a new one. if (!node) { @@ -643,8 +646,9 @@ std::shared_ptr DirNode::findOrCreate(const char *plainName) { node.reset(new FileNode(this, fsConfig, plainName, (rootDir + cipherName).c_str(), fuseFh)); - if (fsConfig->config->externalIVChaining) + if (fsConfig->config->externalIVChaining) { node->setName(nullptr, nullptr, iv); + } VLOG(1) << "created FileNode for " << node->cipherName(); } @@ -673,10 +677,11 @@ std::shared_ptr DirNode::openNode(const char *plainName, std::shared_ptr node = findOrCreate(plainName); - if (node && (*result = node->open(flags)) >= 0) + if (node && (*result = node->open(flags)) >= 0) { return node; - else + } else { return std::shared_ptr(); + } } int DirNode::unlink(const char *plaintextName) { @@ -686,7 +691,7 @@ int DirNode::unlink(const char *plaintextName) { Lock _lock(mutex); int res = 0; - if (ctx && ctx->lookupNode(plaintextName)) { + if ((ctx != nullptr) && ctx->lookupNode(plaintextName)) { // If FUSE is running with "hard_remove" option where it doesn't // hide open files for us, then we can't allow an unlink of an open // file.. diff --git a/encfs/FileNode.cpp b/encfs/FileNode.cpp index 1565ef8..81d87f2 100644 --- a/encfs/FileNode.cpp +++ b/encfs/FileNode.cpp @@ -74,8 +74,10 @@ FileNode::FileNode(DirNode *parent_, const FSConfigPtr &cfg, std::shared_ptr rawIO(new RawFileIO(_cname)); io = std::shared_ptr(new CipherFileIO(rawIO, fsConfig)); - if (cfg->config->blockMACBytes || cfg->config->blockMACRandBytes) + if ((cfg->config->blockMACBytes != 0) || + (cfg->config->blockMACRandBytes != 0)) { io = std::shared_ptr(new MACFileIO(io, fsConfig)); + } } FileNode::~FileNode() { @@ -98,23 +100,24 @@ string FileNode::plaintextParent() const { return parentDirectory(_pname); } static bool setIV(const std::shared_ptr &io, uint64_t iv) { struct stat stbuf; - if ((io->getAttr(&stbuf) < 0) || S_ISREG(stbuf.st_mode)) + if ((io->getAttr(&stbuf) < 0) || S_ISREG(stbuf.st_mode)) { return io->setIV(iv); - else + } else { return true; + } } bool FileNode::setName(const char *plaintextName_, const char *cipherName_, uint64_t iv, bool setIVFirst) { // Lock _lock( mutex ); - if (cipherName_) VLOG(1) << "calling setIV on " << cipherName_; + if (cipherName_ != nullptr) VLOG(1) << "calling setIV on " << cipherName_; if (setIVFirst) { if (fsConfig->config->externalIVChaining && !setIV(io, iv)) return false; // now change the name.. - if (plaintextName_) this->_pname = plaintextName_; - if (cipherName_) { + if (plaintextName_ != nullptr) this->_pname = plaintextName_; + if (cipherName_ != nullptr) { this->_cname = cipherName_; io->setFileName(cipherName_); } @@ -122,8 +125,8 @@ bool FileNode::setName(const char *plaintextName_, const char *cipherName_, std::string oldPName = _pname; std::string oldCName = _cname; - if (plaintextName_) this->_pname = plaintextName_; - if (cipherName_) { + if (plaintextName_ != nullptr) this->_pname = plaintextName_; + if (cipherName_ != nullptr) { this->_cname = cipherName_; io->setFileName(cipherName_); } @@ -167,10 +170,11 @@ int FileNode::mknod(mode_t mode, dev_t rdev, uid_t uid, gid_t gid) { if (S_ISREG(mode)) { res = ::open(_cname.c_str(), O_CREAT | O_EXCL | O_WRONLY, mode); if (res >= 0) res = ::close(res); - } else if (S_ISFIFO(mode)) + } else if (S_ISFIFO(mode)) { res = ::mkfifo(_cname.c_str(), mode); - else + } else { res = ::mknod(_cname.c_str(), mode, rdev); + } if (olduid >= 0) setfsuid(olduid); if (oldgid >= 0) setfsgid(oldgid); @@ -242,10 +246,11 @@ int FileNode::sync(bool datasync) { if (fh >= 0) { int res = -EIO; #ifdef linux - if (datasync) + if (datasync) { res = fdatasync(fh); - else + } else { res = fsync(fh); + } #else (void)datasync; // no fdatasync support @@ -256,8 +261,9 @@ int FileNode::sync(bool datasync) { if (res == -1) res = -errno; return res; - } else + } else { return fh; + } } } // namespace encfs diff --git a/encfs/FileUtils.cpp b/encfs/FileUtils.cpp index 23c2e9a..5047928 100644 --- a/encfs/FileUtils.cpp +++ b/encfs/FileUtils.cpp @@ -125,7 +125,7 @@ EncFS_Root::~EncFS_Root() = default; bool fileExists(const char *fileName) { struct stat buf; - if (!lstat(fileName, &buf)) { + if (lstat(fileName, &buf) == 0) { return true; } else { // XXX show perror? @@ -135,7 +135,7 @@ bool fileExists(const char *fileName) { bool isDirectory(const char *fileName) { struct stat buf; - if (!lstat(fileName, &buf)) { + if (lstat(fileName, &buf) == 0) { return S_ISDIR(buf.st_mode); } else { return false; @@ -143,23 +143,25 @@ bool isDirectory(const char *fileName) { } bool isAbsolutePath(const char *fileName) { - if (fileName && fileName[0] != '\0' && fileName[0] == '/') + if ((fileName != nullptr) && fileName[0] != '\0' && fileName[0] == '/') { return true; - else + } else { return false; + } } const char *lastPathElement(const char *name) { const char *loc = strrchr(name, '/'); - return loc ? loc + 1 : name; + return loc != nullptr ? loc + 1 : name; } std::string parentDirectory(const std::string &path) { size_t last = path.find_last_of('/'); - if (last == string::npos) + if (last == string::npos) { return string(""); - else + } else { return path.substr(0, last); + } } bool userAllowMkdir(const char *path, mode_t mode) { @@ -194,8 +196,9 @@ bool userAllowMkdir(int promptno, const char *path, mode_t mode) { if (result < 0) { perror(_("Unable to create directory: ")); return false; - } else + } else { return true; + } } else { // Directory not created, by user request cerr << _("Directory not created.") << "\n"; @@ -208,7 +211,7 @@ bool userAllowMkdir(int promptno, const char *path, mode_t mode) { */ ConfigType readConfig_load(ConfigInfo *nm, const char *path, EncFSConfig *config) { - if (nm->loadFunc) { + if (nm->loadFunc != nullptr) { try { if ((*nm->loadFunc)(path, config, nm)) { config->cfgType = nm->type; @@ -234,7 +237,7 @@ ConfigType readConfig_load(ConfigInfo *nm, const char *path, */ ConfigType readConfig(const string &rootDir, EncFSConfig *config) { ConfigInfo *nm = ConfigFileMapping; - while (nm->fileName) { + while (nm->fileName != nullptr) { // allow environment variable to override default config path if (nm->environmentOverride != nullptr) { char *envFile = getenv(nm->environmentOverride); @@ -250,8 +253,9 @@ ConfigType readConfig(const string &rootDir, EncFSConfig *config) { } // the standard place to look is in the root directory string path = rootDir + nm->fileName; - if (fileExists(path.c_str())) + if (fileExists(path.c_str())) { return readConfig_load(nm, path.c_str(), config); + } ++nm; } @@ -446,8 +450,8 @@ bool saveConfig(ConfigType type, const string &rootDir, bool ok = false; ConfigInfo *nm = ConfigFileMapping; - while (nm->fileName) { - if (nm->type == type && nm->saveFunc) { + while (nm->fileName != nullptr) { + if (nm->type == type && (nm->saveFunc != nullptr)) { string path = rootDir + nm->fileName; if (nm->environmentOverride != nullptr) { // use environment file if specified.. @@ -593,7 +597,8 @@ static Cipher::CipherAlgorithm findCipherAlgorithm(const char *name, Cipher::AlgorithmList algorithms = Cipher::GetAlgorithmList(); Cipher::AlgorithmList::const_iterator it; for (it = algorithms.begin(); it != algorithms.end(); ++it) { - if (!strcmp(name, it->name.c_str()) && it->keyLength.allowed(keySize)) { + if ((strcmp(name, it->name.c_str()) == 0) && + it->keyLength.allowed(keySize)) { return *it; } } @@ -659,8 +664,9 @@ static Cipher::CipherAlgorithm selectCipherAlgorithm() { } it = algorithms.begin(); - while (--cipherNum) // numbering starts at 1 + while (--cipherNum != 0) { // numbering starts at 1 ++it; + } Cipher::CipherAlgorithm alg = *it; @@ -702,8 +708,9 @@ static Interface selectNameCoding() { } it = algorithms.begin(); - while (--algNum) // numbering starts at 1 + while (--algNum != 0) { // numbering starts at 1 ++it; + } // xgroup(setup) cout << autosprintf(_("Selected algorithm \"%s\""), it->name.c_str()) @@ -738,13 +745,13 @@ static int selectKeySize(const Cipher::CipherAlgorithm &alg) { if (numAvail < 5) { // show them all for (int i = 0; i <= numAvail; ++i) { - if (i) cout << ", "; + if (i != 0) cout << ", "; cout << alg.keyLength.min() + i * alg.keyLength.inc(); } } else { // partial for (int i = 0; i < 3; ++i) { - if (i) cout << ", "; + if (i != 0) cout << ", "; cout << alg.keyLength.min() + i * alg.keyLength.inc(); } cout << " ... " << alg.keyLength.max() - alg.keyLength.inc(); @@ -795,8 +802,9 @@ static int selectBlockSize(const Cipher::CipherAlgorithm &alg) { char *res = fgets(answer, sizeof(answer), stdin); cout << "\n"; - if (res != nullptr && atoi(answer) >= alg.blockSize.min()) + if (res != nullptr && atoi(answer) >= alg.blockSize.min()) { blockSize = atoi(answer); + } blockSize = alg.blockSize.closest(blockSize); @@ -818,10 +826,11 @@ static bool boolDefault(const char *prompt, bool defaultValue) { string yesno; - if (defaultValue == true) + if (defaultValue) { yesno = "[y]/n: "; - else + } else { yesno = "y/[n]: "; + } string response; bool value; @@ -875,10 +884,11 @@ static void selectBlockMAC(int *macBytes, int *macRandBytes, bool forceMac) { addMAC = true; } - if (addMAC) + if (addMAC) { *macBytes = 8; - else + } else { *macBytes = 0; + } // xgroup(setup) cout << _( @@ -1057,7 +1067,7 @@ RootPtr createV6Config(EncFS_Context *ctx, /* Reverse mounts are read-only by default (set in main.cpp). * If uniqueIV is off, writing can be allowed, because there * is no header that could be overwritten */ - if (uniqueIV == false) opts->readOnly = false; + if (!uniqueIV) opts->readOnly = false; } } @@ -1085,13 +1095,13 @@ RootPtr createV6Config(EncFS_Context *ctx, /* Reverse mounts are read-only by default (set in main.cpp). * If uniqueIV is off, writing can be allowed, because there * is no header that could be overwritten */ - if (uniqueIV == false) opts->readOnly = false; + if (!uniqueIV) opts->readOnly = false; } else { chainedIV = selectChainedIV(); uniqueIV = selectUniqueIV(true); - if (chainedIV && uniqueIV) + if (chainedIV && uniqueIV) { externalIV = selectExternalChainedIV(); - else { + } else { // xgroup(setup) cout << _("External chained IV disabled, as both 'IV chaining'\n" "and 'unique IV' features are required for this option.") @@ -1172,10 +1182,11 @@ RootPtr createV6Config(EncFS_Context *ctx, if (useStdin) { if (annotate) cerr << "$PROMPT$ new_passwd" << endl; userKey = config->getUserKey(useStdin); - } else if (!passwordProgram.empty()) + } else if (!passwordProgram.empty()) { userKey = config->getUserKey(passwordProgram, rootDir); - else + } else { userKey = config->getNewUserKey(); + } cipher->writeKey(volumeKey, encodedKey, userKey); userKey.reset(); @@ -1234,17 +1245,18 @@ void showFSInfo(const EncFSConfig *config) { config->cipherIface.name().c_str(), config->cipherIface.current(), config->cipherIface.revision(), config->cipherIface.age()); // check if we support this interface.. - if (!cipher) + if (!cipher) { cout << _(" (NOT supported)\n"); - else { + } else { // if we're using a newer interface, show the version number if (config->cipherIface != cipher->interface()) { Interface iface = cipher->interface(); // xgroup(diag) cout << autosprintf(_(" (using %i:%i:%i)\n"), iface.current(), iface.revision(), iface.age()); - } else + } else { cout << "\n"; + } } } { @@ -1266,8 +1278,9 @@ void showFSInfo(const EncFSConfig *config) { Interface iface = nameCoder->interface(); cout << autosprintf(_(" (using %i:%i:%i)\n"), iface.current(), iface.revision(), iface.age()); - } else + } else { cout << "\n"; + } } } { @@ -1276,8 +1289,9 @@ void showFSInfo(const EncFSConfig *config) { if (!cipher) { // xgroup(diag) cout << _(" (NOT supported)\n"); - } else + } else { cout << "\n"; + } } if (config->kdfIterations > 0 && config->salt.size() > 0) { cout << autosprintf(_("Using PBKDF2, with %i iterations"), @@ -1286,7 +1300,7 @@ void showFSInfo(const EncFSConfig *config) { cout << autosprintf(_("Salt Size: %i bits"), (int)(8 * config->salt.size())) << "\n"; } - if (config->blockMACBytes || config->blockMACRandBytes) { + if ((config->blockMACBytes != 0) || (config->blockMACRandBytes != 0)) { if (config->subVersion < 20040813) { cout << autosprintf( // xgroup(diag) @@ -1392,8 +1406,9 @@ CipherKey EncFSConfig::getUserKey(bool useStdin) { if (useStdin) { res = fgets(passBuf, sizeof(passBuf), stdin); // Kill the trailing newline. - if (passBuf[strlen(passBuf) - 1] == '\n') + if (passBuf[strlen(passBuf) - 1] == '\n') { passBuf[strlen(passBuf) - 1] = '\0'; + } } else { // xgroup(common) res = readpassphrase(_("EncFS Password: "), passBuf, sizeof(passBuf), @@ -1401,7 +1416,7 @@ CipherKey EncFSConfig::getUserKey(bool useStdin) { } CipherKey userKey; - if (!res) { + if (res == nullptr) { cerr << _("fatal: error reading password\n"); exit(1); } else { @@ -1423,15 +1438,17 @@ std::string readPassword(int FD) { if (rdSize > 0) { result.append(buffer, rdSize); memset(buffer, 0, sizeof(buffer)); - } else + } else { break; + } } // chop off trailing "\n" if present.. // This is done so that we can use standard programs like ssh-askpass // without modification, as it returns trailing newline.. - if (!result.empty() && result[result.length() - 1] == '\n') + if (!result.empty() && result[result.length() - 1] == '\n') { result.resize(result.length() - 1); + } return result; } @@ -1525,7 +1542,8 @@ CipherKey EncFSConfig::getNewUserKey() { char *res2 = readpassphrase(_("Verify Encfs Password: "), passBuf2, sizeof(passBuf2) - 1, RPP_ECHO_OFF); - if (res1 && res2 && !strcmp(passBuf, passBuf2)) { + if ((res1 != nullptr) && (res2 != nullptr) && + (strcmp(passBuf, passBuf2) == 0)) { userKey = makeKey(passBuf, strlen(passBuf)); } else { // xgroup(common) -- probably not common, but group with the others @@ -1560,7 +1578,7 @@ RootPtr initFS(EncFS_Context *ctx, const std::shared_ptr &opts) { /* Reverse mounts are read-only by default (set in main.cpp). * If uniqueIV is off, writing can be allowed, because there * is no header that could be overwritten */ - if (config->uniqueIV == false) opts->readOnly = false; + if (!config->uniqueIV) opts->readOnly = false; } // first, instanciate the cipher. @@ -1589,8 +1607,9 @@ RootPtr initFS(EncFS_Context *ctx, const std::shared_ptr &opts) { VLOG(1) << "useStdin: " << opts->useStdin; if (opts->annotate) cerr << "$PROMPT$ passwd" << endl; userKey = config->getUserKey(opts->useStdin); - } else + } else { userKey = config->getUserKey(opts->passwordProgram, opts->rootDir); + } if (!userKey) return rootInfo; diff --git a/encfs/Interface.cpp b/encfs/Interface.cpp index 18fb6e0..4f996e6 100644 --- a/encfs/Interface.cpp +++ b/encfs/Interface.cpp @@ -83,12 +83,13 @@ static int sign( int a, int b ) #else // simple, easy to check, unlikely to break due to unforseen events.. static int sign(int a, int b) { - if (a < b) + if (a < b) { return 0; - else if (a == b) + } else if (a == b) { return 1; - else + } else { return 2; + } } #endif @@ -116,29 +117,33 @@ bool Interface::implements(const Interface &B) const { bool operator<(const Interface &A, const Interface &B) { if (A.name() == B.name()) { return (diffSum(A, B) < EqualVersion); - } else + } else { return A.name() < B.name(); + } } bool operator>(const Interface &A, const Interface &B) { if (A.name() == B.name()) { return (diffSum(A, B) > EqualVersion); - } else + } else { return A.name() < B.name(); + } } bool operator<=(const Interface &A, const Interface &B) { if (A.name() == B.name()) { return (diffSum(A, B) <= EqualVersion); - } else + } else { return A.name() < B.name(); + } } bool operator>=(const Interface &A, const Interface &B) { if (A.name() == B.name()) { return (diffSum(A, B) >= EqualVersion); - } else + } else { return A.name() < B.name(); + } } ConfigVar &operator<<(ConfigVar &dst, const Interface &iface) { diff --git a/encfs/MACFileIO.cpp b/encfs/MACFileIO.cpp index e03a618..4e2e350 100644 --- a/encfs/MACFileIO.cpp +++ b/encfs/MACFileIO.cpp @@ -160,13 +160,15 @@ ssize_t MACFileIO::readOneBlock(const IORequest &req) const { // don't store zeros if configured for zero-block pass-through bool skipBlock = true; if (_allowHoles) { - for (int i = 0; i < readSize; ++i) + for (int i = 0; i < readSize; ++i) { if (tmp.data[i] != 0) { skipBlock = false; break; } - } else if (macBytes > 0) + } + } else if (macBytes > 0) { skipBlock = false; + } if (readSize > headerSize) { if (!skipBlock) { @@ -224,8 +226,9 @@ bool MACFileIO::writeOneBlock(const IORequest &req) { memset(newReq.data, 0, headerSize); memcpy(newReq.data + headerSize, req.data, req.dataLen); if (randBytes > 0) { - if (!cipher->randomize(newReq.data + macBytes, randBytes, false)) + if (!cipher->randomize(newReq.data + macBytes, randBytes, false)) { return false; + } } if (macBytes > 0) { diff --git a/encfs/MemoryPool.cpp b/encfs/MemoryPool.cpp index 6291785..b76b212 100644 --- a/encfs/MemoryPool.cpp +++ b/encfs/MemoryPool.cpp @@ -75,15 +75,16 @@ MemBlock MemoryPool::allocate(int size) { } // unlink block from list - if (block) { - if (!parent) + if (block != nullptr) { + if (parent == nullptr) { gMemPool = block->next; - else + } else { parent->next = block->next; + } } pthread_mutex_unlock(&gMPoolMutex); - if (!block) block = allocBlock(size); + if (block == nullptr) block = allocBlock(size); block->next = nullptr; MemBlock result; diff --git a/encfs/NameIO.cpp b/encfs/NameIO.cpp index 9cdb1c5..98d2f2b 100644 --- a/encfs/NameIO.cpp +++ b/encfs/NameIO.cpp @@ -62,7 +62,7 @@ list NameIO::GetAlgorithmList(bool includeHidden) { AddSymbolReferences(); list result; - if (gNameIOMap) { + if (gNameIOMap != nullptr) { NameIOMap_t::const_iterator it; NameIOMap_t::const_iterator end = gNameIOMap->end(); for (it = gNameIOMap->begin(); it != end; ++it) { @@ -83,7 +83,7 @@ list NameIO::GetAlgorithmList(bool includeHidden) { bool NameIO::Register(const char *name, const char *description, const Interface &iface, Constructor constructor, bool hidden) { - if (!gNameIOMap) gNameIOMap = new NameIOMap_t; + if (gNameIOMap == nullptr) gNameIOMap = new NameIOMap_t; NameIOAlg alg; alg.hidden = hidden; @@ -98,7 +98,7 @@ std::shared_ptr NameIO::New(const string &name, const std::shared_ptr &cipher, const CipherKey &key) { std::shared_ptr result; - if (gNameIOMap) { + if (gNameIOMap != nullptr) { NameIOMap_t::const_iterator it = gNameIOMap->find(name); if (it != gNameIOMap->end()) { Constructor fn = it->second.constructor; @@ -111,7 +111,7 @@ std::shared_ptr NameIO::New(const Interface &iface, const std::shared_ptr &cipher, const CipherKey &key) { std::shared_ptr result; - if (gNameIOMap) { + if (gNameIOMap != nullptr) { NameIOMap_t::const_iterator it; NameIOMap_t::const_iterator end = gNameIOMap->end(); for (it = gNameIOMap->begin(); it != end; ++it) { @@ -143,15 +143,16 @@ std::string NameIO::recodePath( uint64_t *iv) const { string output; - while (*path) { + while (*path != 0) { if (*path == '/') { - if (!output.empty()) // don't start the string with '/' + if (!output.empty()) { // don't start the string with '/' output += '/'; + } ++path; } else { bool isDotFile = (*path == '.'); const char *next = strchr(path, '/'); - int len = next ? next - path : strlen(path); + int len = next != nullptr ? next - path : strlen(path); // at this point we know that len > 0 if (isDotFile && (path[len - 1] == '.') && (len <= 2)) { diff --git a/encfs/RawFileIO.cpp b/encfs/RawFileIO.cpp index fdfaccd..e9a5c4b 100644 --- a/encfs/RawFileIO.cpp +++ b/encfs/RawFileIO.cpp @@ -113,7 +113,7 @@ static int open_readonly_workaround(const char *path, int flags) { it.. */ int RawFileIO::open(int flags) { - bool requestWrite = ((flags & O_RDWR) || (flags & O_WRONLY)); + bool requestWrite = (((flags & O_RDWR) != 0) || ((flags & O_WRONLY) != 0)); VLOG(1) << "open call, requestWrite = " << requestWrite; int result = 0; @@ -126,7 +126,7 @@ int RawFileIO::open(int flags) { int finalFlags = requestWrite ? O_RDWR : O_RDONLY; #if defined(O_LARGEFILE) - if (flags & O_LARGEFILE) finalFlags |= O_LARGEFILE; + if ((flags & O_LARGEFILE) != 0) finalFlags |= O_LARGEFILE; #else #warning O_LARGEFILE not supported #endif @@ -209,14 +209,14 @@ ssize_t RawFileIO::read(const IORequest &req) const { bool RawFileIO::write(const IORequest &req) { rAssert(fd >= 0); - rAssert(true == canWrite); + rAssert(canWrite); int retrys = 10; void *buf = req.data; ssize_t bytes = req.dataLen; off_t offset = req.offset; - while (bytes && retrys > 0) { + while ((bytes != 0) && retrys > 0) { ssize_t writeSize = ::pwrite(fd, buf, bytes, offset); if (writeSize < 0) { @@ -255,8 +255,9 @@ int RawFileIO::truncate(off_t size) { #if !defined(__FreeBSD__) && !defined(__APPLE__) ::fdatasync(fd); #endif - } else + } else { res = ::truncate(name.c_str(), size); + } if (res < 0) { int eno = errno; diff --git a/encfs/SSL_Cipher.cpp b/encfs/SSL_Cipher.cpp index 7c6c749..62fd6f2 100644 --- a/encfs/SSL_Cipher.cpp +++ b/encfs/SSL_Cipher.cpp @@ -66,21 +66,22 @@ inline int MIN(int a, int b) { return (a < b) ? a : b; } int BytesToKey(int keyLen, int ivLen, const EVP_MD *md, const unsigned char *data, int dataLen, unsigned int rounds, unsigned char *key, unsigned char *iv) { - if (data == nullptr || dataLen == 0) + if (data == nullptr || dataLen == 0) { return 0; // OpenSSL returns nkey here, but why? It is a failure.. + } unsigned char mdBuf[EVP_MAX_MD_SIZE]; unsigned int mds = 0; int addmd = 0; - int nkey = key ? keyLen : 0; - int niv = iv ? ivLen : 0; + int nkey = key != nullptr ? keyLen : 0; + int niv = iv != nullptr ? ivLen : 0; EVP_MD_CTX *cx = EVP_MD_CTX_new(); EVP_MD_CTX_init(cx); for (;;) { EVP_DigestInit_ex(cx, md, nullptr); - if (addmd++) EVP_DigestUpdate(cx, mdBuf, mds); + if ((addmd++) != 0) EVP_DigestUpdate(cx, mdBuf, mds); EVP_DigestUpdate(cx, data, dataLen); EVP_DigestFinal_ex(cx, mdBuf, &mds); @@ -92,14 +93,14 @@ int BytesToKey(int keyLen, int ivLen, const EVP_MD *md, int offset = 0; int toCopy = MIN(nkey, mds - offset); - if (toCopy) { + if (toCopy != 0) { memcpy(key, mdBuf + offset, toCopy); key += toCopy; nkey -= toCopy; offset += toCopy; } toCopy = MIN(niv, mds - offset); - if (toCopy) { + if (toCopy != 0) { memcpy(iv, mdBuf + offset, toCopy); iv += toCopy; niv -= toCopy; @@ -139,8 +140,9 @@ int TimedPBKDF2(const char *pass, int passlen, const unsigned char *salt, } else if (delta < (5 * desiredPDFTime / 6)) { // estimate number of iterations to get close to desired time iter = (int)((double)iter * (double)desiredPDFTime / (double)delta); - } else + } else { return iter; + } } } @@ -366,8 +368,9 @@ CipherKey SSL_Cipher::newKey(const char *password, int passwdLength, if (res <= 0) { RLOG(WARNING) << "openssl error, PBKDF2 failed"; return CipherKey(); - } else + } else { iterationCount = res; + } } else { // known iteration length if (PKCS5_PBKDF2_HMAC_SHA1( @@ -425,8 +428,9 @@ CipherKey SSL_Cipher::newRandomKey() { int saltLen = 20; unsigned char saltBuf[saltLen]; - if (!randomize(tmpBuf, bufLen, true) || !randomize(saltBuf, saltLen, true)) + if (!randomize(tmpBuf, bufLen, true) || !randomize(saltBuf, saltLen, true)) { return CipherKey(); + } std::shared_ptr key(new SSLKey(_keySize, _ivLength)); @@ -458,7 +462,7 @@ static uint64_t _checksum_64(SSLKey *key, const unsigned char *data, HMAC_Init_ex(key->mac_ctx, nullptr, 0, nullptr, nullptr); HMAC_Update(key->mac_ctx, data, dataLen); - if (chainedIV) { + if (chainedIV != nullptr) { // toss in the chained IV as well uint64_t tmp = *chainedIV; unsigned char h[8]; @@ -476,8 +480,9 @@ static uint64_t _checksum_64(SSLKey *key, const unsigned char *data, // chop this down to a 64bit value.. unsigned char h[8] = {0, 0, 0, 0, 0, 0, 0, 0}; - for (unsigned int i = 0; i < (mdLen - 1); ++i) + for (unsigned int i = 0; i < (mdLen - 1); ++i) { h[i % 8] ^= (unsigned char)(md[i]); + } auto value = (uint64_t)h[0]; for (int i = 1; i < 8; ++i) value = (value << 8) | (uint64_t)h[i]; @@ -515,7 +520,7 @@ uint64_t SSL_Cipher::MAC_64(const unsigned char *data, int len, std::shared_ptr mk = dynamic_pointer_cast(key); uint64_t tmp = _checksum_64(mk.get(), data, len, chainedIV); - if (chainedIV) *chainedIV = tmp; + if (chainedIV != nullptr) *chainedIV = tmp; return tmp; } @@ -529,8 +534,9 @@ CipherKey SSL_Cipher::readKey(const unsigned char *data, // First N bytes are checksum bytes. unsigned int checksum = 0; - for (int i = 0; i < KEY_CHECKSUM_BYTES; ++i) + for (int i = 0; i < KEY_CHECKSUM_BYTES; ++i) { checksum = (checksum << 8) | (unsigned int)data[i]; + } memcpy(tmpBuf, data + KEY_CHECKSUM_BYTES, _keySize + _ivLength); streamDecode(tmpBuf, _keySize + _ivLength, checksum, masterKey); @@ -591,10 +597,11 @@ bool SSL_Cipher::compareKey(const CipherKey &A, const CipherKey &B) const { rAssert(key1->keySize == _keySize); rAssert(key2->keySize == _keySize); - if (memcmp(key1->buffer, key2->buffer, _keySize + _ivLength) != 0) + if (memcmp(key1->buffer, key2->buffer, _keySize + _ivLength) != 0) { return false; - else + } else { return true; + } } int SSL_Cipher::encodedKeySize() const { @@ -694,7 +701,7 @@ static void flipBytes(unsigned char *buf, int size) { unsigned char revBuf[64]; int bytesLeft = size; - while (bytesLeft) { + while (bytesLeft != 0) { int toFlip = MIN(sizeof(revBuf), bytesLeft); for (int i = 0; i < toFlip; ++i) revBuf[i] = buf[toFlip - (i + 1)]; @@ -711,7 +718,7 @@ static void shuffleBytes(unsigned char *buf, int size) { } static void unshuffleBytes(unsigned char *buf, int size) { - for (int i = size - 1; i; --i) buf[i] ^= buf[i - 1]; + for (int i = size - 1; i != 0; --i) buf[i] ^= buf[i - 1]; } /** Partial blocks are encoded with a stream cipher. We make multiple passes on @@ -798,8 +805,9 @@ bool SSL_Cipher::blockEncode(unsigned char *buf, int size, uint64_t iv64, // data must be integer number of blocks const int blockMod = size % EVP_CIPHER_CTX_block_size(key->block_enc); - if (blockMod != 0) + if (blockMod != 0) { throw Error("Invalid data size, not multiple of block size"); + } Lock lock(key->mutex); @@ -830,8 +838,9 @@ bool SSL_Cipher::blockDecode(unsigned char *buf, int size, uint64_t iv64, // data must be integer number of blocks const int blockMod = size % EVP_CIPHER_CTX_block_size(key->block_dec); - if (blockMod != 0) + if (blockMod != 0) { throw Error("Invalid data size, not multiple of block size"); + } Lock lock(key->mutex); diff --git a/encfs/StreamNameIO.cpp b/encfs/StreamNameIO.cpp index ec06852..a2b1649 100644 --- a/encfs/StreamNameIO.cpp +++ b/encfs/StreamNameIO.cpp @@ -96,7 +96,7 @@ int StreamNameIO::encodeName(const char *plaintextName, int length, uint64_t *iv, char *encodedName, int bufferLength) const { uint64_t tmpIV = 0; - if (iv && _interface >= 2) tmpIV = *iv; + if ((iv != nullptr) && _interface >= 2) tmpIV = *iv; unsigned int mac = _cipher->MAC_16((const unsigned char *)plaintextName, length, _key, iv); @@ -155,7 +155,7 @@ int StreamNameIO::decodeName(const char *encodedName, int length, uint64_t *iv, ((unsigned int)((unsigned char)tmpBuf[1])); // version 2 adds support for IV chaining.. - if (iv && _interface >= 2) tmpIV = *iv; + if ((iv != nullptr) && _interface >= 2) tmpIV = *iv; memcpy(plaintextName, tmpBuf + 2, decodedStreamLen); } else { diff --git a/encfs/XmlReader.cpp b/encfs/XmlReader.cpp index a3cd9a6..de0dfa5 100644 --- a/encfs/XmlReader.cpp +++ b/encfs/XmlReader.cpp @@ -80,7 +80,7 @@ bool XmlValue::read(const char *path, bool *out) const { XmlValuePtr value = find(path); if (!value) return false; - *out = atoi(value->text().c_str()); + *out = (atoi(value->text().c_str()) != 0); return true; } @@ -124,9 +124,9 @@ std::string safeValueForNode(const tinyxml2::XMLElement *element) { if (element == nullptr) return value; const tinyxml2::XMLNode *child = element->FirstChild(); - if (child) { + if (child != nullptr) { const tinyxml2::XMLText *childText = child->ToText(); - if (childText) value = childText->Value(); + if (childText != nullptr) value = childText->Value(); } return value; @@ -144,16 +144,18 @@ class XmlNode : virtual public XmlValue { XmlValuePtr find(const char *name) const override { if (name[0] == '@') { const char *value = element->Attribute(name + 1); - if (value) + if (value != nullptr) { return std::make_shared(value); - else + } else { return XmlValuePtr(); + } } else { const tinyxml2::XMLElement *el = element->FirstChildElement(name); - if (el) + if (el != nullptr) { return XmlValuePtr(new XmlNode(el)); - else + } else { return XmlValuePtr(); + } } } }; diff --git a/encfs/autosprintf.cpp b/encfs/autosprintf.cpp index fbb164b..6654b1c 100644 --- a/encfs/autosprintf.cpp +++ b/encfs/autosprintf.cpp @@ -57,10 +57,11 @@ autosprintf::operator char *() const { auto *copy = new char[length]; memcpy(copy, str, length); return copy; - } else + } else { return nullptr; + } } autosprintf::operator std::string() const { - return std::string(str ? str : "(error in autosprintf)"); -} + return std::string(str != nullptr ? str : "(error in autosprintf)"); } +} // namespace gnu diff --git a/encfs/base64.cpp b/encfs/base64.cpp index 9edf8ca..38de109 100644 --- a/encfs/base64.cpp +++ b/encfs/base64.cpp @@ -52,7 +52,7 @@ void changeBase2(unsigned char *src, int srcLen, int src2Pow, } // now, we could have a partial value left in the work buffer.. - if (workBits && ((dst - origDst) < dstLen)) *dst++ = work & mask; + if ((workBits != 0) && ((dst - origDst) < dstLen)) *dst++ = work & mask; } /* @@ -67,11 +67,11 @@ static void changeBase2Inline(unsigned char *src, int srcLen, int src2Pow, unsigned long work, int workBits, unsigned char *outLoc) { const int mask = (1 << dst2Pow) - 1; - if (!outLoc) outLoc = src; + if (outLoc == nullptr) outLoc = src; // copy the new bits onto the high bits of the stream. // The bits that fall off the low end are the output bits. - while (srcLen && workBits < dst2Pow) { + while ((srcLen != 0) && workBits < dst2Pow) { work |= ((unsigned long)(*src++)) << workBits; workBits += src2Pow; --srcLen; @@ -82,7 +82,7 @@ static void changeBase2Inline(unsigned char *src, int srcLen, int src2Pow, work >>= dst2Pow; workBits -= dst2Pow; - if (srcLen) { + if (srcLen != 0) { // more input left, so recurse changeBase2Inline(src, srcLen, src2Pow, dst2Pow, outputPartialLastByte, work, workBits, outLoc + 1); @@ -119,12 +119,14 @@ void B64ToAscii(unsigned char *in, int length) { for (int offset = 0; offset < length; ++offset) { int ch = in[offset]; if (ch > 11) { - if (ch > 37) + if (ch > 37) { ch += 'a' - 38; - else + } else { ch += 'A' - 12; - } else + } + } else { ch = B642AsciiTable[ch]; + } in[offset] = ch; } @@ -139,16 +141,17 @@ void AsciiToB64(unsigned char *in, int length) { } void AsciiToB64(unsigned char *out, const unsigned char *in, int length) { - while (length--) { + while ((length--) != 0) { unsigned char ch = *in++; if (ch >= 'A') { - if (ch >= 'a') + if (ch >= 'a') { ch += 38 - 'a'; - else + } else { ch += 12 - 'A'; - } else + } + } else { ch = Ascii2B64Table[ch] - '0'; - + } *out++ = ch; } } @@ -156,10 +159,11 @@ void AsciiToB64(unsigned char *out, const unsigned char *in, int length) { void B32ToAscii(unsigned char *buf, int len) { for (int offset = 0; offset < len; ++offset) { int ch = buf[offset]; - if (ch >= 0 && ch < 26) + if (ch >= 0 && ch < 26) { ch += 'A'; - else + } else { ch += '2' - 26; + } buf[offset] = ch; } @@ -170,14 +174,14 @@ void AsciiToB32(unsigned char *in, int length) { } void AsciiToB32(unsigned char *out, const unsigned char *in, int length) { - while (length--) { + while ((length--) != 0) { unsigned char ch = *in++; int lch = toupper(ch); - if (lch >= 'A') + if (lch >= 'A') { lch -= 'A'; - else + } else { lch += 26 - '2'; - + } *out++ = (unsigned char)lch; } } @@ -221,7 +225,7 @@ bool B64StandardDecode(unsigned char *out, const unsigned char *in, int inLen) { buf = buf << 6 | c; /* If the buffer is full, split it into bytes */ - if (buf & 0x1000000) { + if ((buf & 0x1000000) != 0u) { *out++ = buf >> 16; *out++ = buf >> 8; *out++ = buf; @@ -230,10 +234,10 @@ bool B64StandardDecode(unsigned char *out, const unsigned char *in, int inLen) { } } - if (buf & 0x40000) { + if ((buf & 0x40000) != 0u) { *out++ = buf >> 10; *out++ = buf >> 2; - } else if (buf & 0x1000) { + } else if ((buf & 0x1000) != 0u) { *out++ = buf >> 4; } diff --git a/encfs/encfs.cpp b/encfs/encfs.cpp index 07d1451..c3ec08c 100644 --- a/encfs/encfs.cpp +++ b/encfs/encfs.cpp @@ -252,7 +252,7 @@ int encfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, #if defined(fuse_fill_dir_flags) if (filler(buf, name.c_str(), &st, 0, 0)) break; #else - if (filler(buf, name.c_str(), &st, 0)) break; + if (filler(buf, name.c_str(), &st, 0) != 0) break; #endif name = dt.nextPlaintextName(&fileType, &inode); @@ -300,8 +300,9 @@ int encfs_mknod(const char *path, mode_t mode, dev_t rdev) { FSRoot->lookupNode(parent.c_str(), "mknod"); struct stat st; - if (dnode->getAttr(&st) == 0) + if (dnode->getAttr(&st) == 0) { res = fnode->mknod(mode, rdev, uid, st.st_gid); + } } } catch (encfs::Error &err) { RLOG(ERROR) << "error caught in mknod: " << err.what(); @@ -335,8 +336,9 @@ int encfs_mkdir(const char *path, mode_t mode) { FSRoot->lookupNode(parent.c_str(), "mkdir"); struct stat st; - if (dnode->getAttr(&st) == 0) + if (dnode->getAttr(&st) == 0) { res = FSRoot->mkdir(path, mode, uid, st.st_gid); + } } } catch (encfs::Error &err) { RLOG(ERROR) << "error caught in mkdir: " << err.what(); @@ -438,10 +440,11 @@ int encfs_symlink(const char *to, const char *from) { if (olduid >= 0) setfsuid(olduid); if (oldgid >= 0) setfsgid(oldgid); - if (res == -1) + if (res == -1) { res = -errno; - else + } else { res = ESUCCESS; + } } catch (encfs::Error &err) { RLOG(ERROR) << "error caught in symlink: " << err.what(); } @@ -547,8 +550,10 @@ int encfs_utimens(const char *path, const struct timespec ts[2]) { int encfs_open(const char *path, struct fuse_file_info *file) { EncFS_Context *ctx = context(); - if (isReadOnly(ctx) && (file->flags & O_WRONLY || file->flags & O_RDWR)) + if (isReadOnly(ctx) && + (((file->flags & O_WRONLY) != 0) || ((file->flags & O_RDWR) != 0))) { return -EROFS; + } int res = -EIO; std::shared_ptr FSRoot = ctx->getRoot(&res); @@ -577,7 +582,7 @@ int encfs_open(const char *path, struct fuse_file_info *file) { int encfs_create(const char *path, mode_t mode, struct fuse_file_info *file) { int res = encfs_mknod(path, mode, 0); - if (res) { + if (res != 0) { return res; } @@ -648,10 +653,11 @@ int encfs_fsync(const char *path, int dataSync, struct fuse_file_info *file) { } int _do_write(FileNode *fnode, unsigned char *ptr, size_t size, off_t offset) { - if (fnode->write(offset, ptr, size)) + if (fnode->write(offset, ptr, size)) { return size; - else + } else { return -EIO; + } } int encfs_write(const char *path, const char *buf, size_t size, off_t offset, @@ -673,7 +679,7 @@ int encfs_statfs(const char *path, struct statvfs *st) { VLOG(1) << "doing statfs of " << cyName; res = statvfs(cyName.c_str(), st); - if (!res) { + if (res == 0) { // adjust maximum name length.. st->f_namemax = 6 * (st->f_namemax - 2) / 8; // approx.. } diff --git a/encfs/main.cpp b/encfs/main.cpp index 104ba38..98bcf11 100644 --- a/encfs/main.cpp +++ b/encfs/main.cpp @@ -94,8 +94,9 @@ struct EncFS_Args { if (opts->reverseEncryption) ss << "(reverseEncryption) "; if (opts->mountOnDemand) ss << "(mountOnDemand) "; if (opts->delayMount) ss << "(delayMount) "; - for (int i = 0; i < fuseArgc; ++i) ss << fuseArgv[i] << ' '; - + for (int i = 0; i < fuseArgc; ++i) { + ss << fuseArgv[i] << ' '; + } return ss.str(); } @@ -343,9 +344,9 @@ static bool processArgs(int argc, char *argv[], out->opts->passwordProgram.assign(optarg); break; case 'P': - if (geteuid() != 0) + if (geteuid() != 0) { RLOG(WARNING) << "option '--public' ignored for non-root user"; - else { + } else { out->opts->ownerCreate = true; // add 'allow_other' option // add 'default_permissions' option (default) @@ -496,7 +497,7 @@ void *encfs_init(fuse_conn_info *conn) { auto *ctx = (EncFS_Context *)fuse_get_context()->private_data; // set fuse connection options - conn->async_read = true; + conn->async_read = 1u; // if an idle timeout is specified, then setup a thread to monitor the // filesystem. @@ -534,8 +535,9 @@ int main(int argc, char *argv[]) { // anything that comes from the user should be considered tainted until // we've processed it and only allowed through what we support. std::shared_ptr encfsArgs(new EncFS_Args); - for (int i = 0; i < MaxFuseArgs; ++i) + for (int i = 0; i < MaxFuseArgs; ++i) { encfsArgs->fuseArgv[i] = nullptr; // libfuse expects null args.. + } if (argc == 1 || !processArgs(argc, argv, encfsArgs)) { usage(argv[0]); @@ -616,7 +618,7 @@ int main(int argc, char *argv[]) { ctx->args = encfsArgs; ctx->opts = encfsArgs->opts; - if (encfsArgs->isThreaded == false && encfsArgs->idleTimeout > 0) { + if (!encfsArgs->isThreaded && encfsArgs->idleTimeout > 0) { // xgroup(usage) cerr << _("Note: requested single-threaded mode, but an idle\n" "timeout was specified. The filesystem will operate\n" @@ -727,12 +729,13 @@ static void *idleMonitor(void *_arg) { int usage, openCount; ctx->getAndResetUsageCounter(&usage, &openCount); - if (usage == 0 && ctx->isMounted()) + if (usage == 0 && ctx->isMounted()) { ++idleCycles; - else { - if (idleCycles >= timeoutCycles) + } else { + if (idleCycles >= timeoutCycles) { RLOG(INFO) << "Filesystem no longer inactive: " << arg->opts->mountPoint; + } idleCycles = 0; } @@ -765,8 +768,9 @@ static void *idleMonitor(void *_arg) { // If we are here FS has been unmounted, so if we did not unmount ourselves // (manual, kill...), notify - if (!unmountres) + if (!unmountres) { RLOG(INFO) << "Filesystem unmounted: " << arg->opts->mountPoint; + } VLOG(1) << "Idle monitoring thread exiting"; diff --git a/encfs/openssl.cpp b/encfs/openssl.cpp index f336a6b..c1761ba 100644 --- a/encfs/openssl.cpp +++ b/encfs/openssl.cpp @@ -43,14 +43,15 @@ void pthreads_locking_callback(int mode, int n, const char *caller_file, (void)caller_file; (void)caller_line; - if (!crypto_locks) { + if (crypto_locks == nullptr) { VLOG(1) << "Allocating " << CRYPTO_num_locks() << " locks for OpenSSL"; crypto_locks = new pthread_mutex_t[CRYPTO_num_locks()]; - for (int i = 0; i < CRYPTO_num_locks(); ++i) + for (int i = 0; i < CRYPTO_num_locks(); ++i) { pthread_mutex_init(crypto_locks + i, nullptr); + } } - if (mode & CRYPTO_LOCK) { + if ((mode & CRYPTO_LOCK) != 0) { pthread_mutex_lock(crypto_locks + n); } else { pthread_mutex_unlock(crypto_locks + n); @@ -58,9 +59,10 @@ void pthreads_locking_callback(int mode, int n, const char *caller_file, } void pthreads_locking_cleanup() { - if (crypto_locks) { - for (int i = 0; i < CRYPTO_num_locks(); ++i) + if (crypto_locks != nullptr) { + for (int i = 0; i < CRYPTO_num_locks(); ++i) { pthread_mutex_destroy(crypto_locks + i); + } delete[] crypto_locks; crypto_locks = nullptr; } diff --git a/encfs/readpassphrase.cpp b/encfs/readpassphrase.cpp index a439e67..ebbafe1 100644 --- a/encfs/readpassphrase.cpp +++ b/encfs/readpassphrase.cpp @@ -85,7 +85,7 @@ restart: * stdin and write to stderr unless a tty is required. */ if ((input = output = open(_PATH_TTY, O_RDWR)) == -1) { - if (flags & RPP_REQUIRE_TTY) { + if ((flags & RPP_REQUIRE_TTY) != 0) { errno = ENOTTY; return (nullptr); } @@ -112,7 +112,7 @@ restart: /* Turn off echo if possible. */ if (tcgetattr(input, &oterm) == 0) { memcpy(&term, &oterm, sizeof(term)); - if (!(flags & RPP_ECHO_ON)) term.c_lflag &= ~(ECHO | ECHONL); + if ((flags & RPP_ECHO_ON) == 0) term.c_lflag &= ~(ECHO | ECHONL); #ifdef VSTATUS if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) term.c_cc[VSTATUS] = _POSIX_VDISABLE; @@ -127,21 +127,22 @@ restart: end = buf + bufsiz - 1; for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { if (p < end) { - if ((flags & RPP_SEVENBIT)) ch &= 0x7f; - if (isalpha(ch)) { - if ((flags & RPP_FORCELOWER)) ch = tolower(ch); - if ((flags & RPP_FORCEUPPER)) ch = toupper(ch); + if ((flags & RPP_SEVENBIT) != 0) ch &= 0x7f; + if (isalpha(ch) != 0) { + if ((flags & RPP_FORCELOWER) != 0) ch = tolower(ch); + if ((flags & RPP_FORCEUPPER) != 0) ch = toupper(ch); } *p++ = ch; } } *p = '\0'; save_errno = errno; - if (!(term.c_lflag & ECHO)) (void)write(output, "\n", 1); + if ((term.c_lflag & ECHO) == 0u) (void)write(output, "\n", 1); /* Restore old terminal settings and signals. */ - if (memcmp(&term, &oterm, sizeof(term)) != 0) + if (memcmp(&term, &oterm, sizeof(term)) != 0) { (void)tcsetattr(input, _T_FLUSH, &oterm); + } (void)sigaction(SIGINT, &saveint, nullptr); (void)sigaction(SIGHUP, &savehup, nullptr); (void)sigaction(SIGQUIT, &savequit, nullptr); @@ -155,7 +156,7 @@ restart: * If we were interrupted by a signal, resend it to ourselves * now that we have restored the signal handlers. */ - if (signo) { + if (signo != 0) { kill(getpid(), signo); switch (signo) { case SIGTSTP: From f9d22c4f188113785b1a18e786eeaa6373cb8d1c Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Thu, 3 Aug 2017 22:43:14 -0700 Subject: [PATCH 07/10] modernize: unindent else after return --- encfs/BlockFileIO.cpp | 112 ++++++++++++++++++++--------------------- encfs/BlockNameIO.cpp | 6 +-- encfs/CipherFileIO.cpp | 41 +++++++-------- encfs/ConfigReader.cpp | 3 +- encfs/ConfigVar.cpp | 6 +-- encfs/DirNode.cpp | 34 ++++++------- encfs/FileNode.cpp | 6 +-- encfs/FileUtils.cpp | 40 +++++++-------- encfs/Interface.cpp | 20 +++----- encfs/RawFileIO.cpp | 21 ++++---- encfs/SSL_Cipher.cpp | 10 ++-- encfs/XmlReader.cpp | 15 +++--- encfs/autosprintf.cpp | 3 +- encfs/encfs.cpp | 8 ++- encfs/main.cpp | 13 +++-- 15 files changed, 148 insertions(+), 190 deletions(-) diff --git a/encfs/BlockFileIO.cpp b/encfs/BlockFileIO.cpp index 89c035e..0ce9501 100644 --- a/encfs/BlockFileIO.cpp +++ b/encfs/BlockFileIO.cpp @@ -73,25 +73,24 @@ ssize_t BlockFileIO::cacheReadOneBlock(const IORequest &req) const { if (_cache.dataLen < len) len = _cache.dataLen; // Don't read past EOF memcpy(req.data, _cache.data, len); return len; - } else { - if (_cache.dataLen > 0) clearCache(_cache, _blockSize); - - // cache results of read -- issue reads for full blocks - IORequest tmp; - tmp.offset = req.offset; - tmp.data = _cache.data; - tmp.dataLen = _blockSize; - ssize_t result = readOneBlock(tmp); - if (result > 0) { - _cache.offset = req.offset; - _cache.dataLen = result; // the amount we really have - if (result > req.dataLen) { - result = req.dataLen; // only as much as requested - } - memcpy(req.data, _cache.data, result); - } - return result; } + if (_cache.dataLen > 0) clearCache(_cache, _blockSize); + + // cache results of read -- issue reads for full blocks + IORequest tmp; + tmp.offset = req.offset; + tmp.data = _cache.data; + tmp.dataLen = _blockSize; + ssize_t result = readOneBlock(tmp); + if (result > 0) { + _cache.offset = req.offset; + _cache.dataLen = result; // the amount we really have + if (result > req.dataLen) { + result = req.dataLen; // only as much as requested + } + memcpy(req.data, _cache.data, result); + } + return result; } bool BlockFileIO::cacheWriteOneBlock(const IORequest &req) { @@ -123,52 +122,51 @@ ssize_t BlockFileIO::read(const IORequest &req) const { // read completely within a single block -- can be handled as-is by // readOneBlock(). return cacheReadOneBlock(req); - } else { - size_t size = req.dataLen; + } + size_t size = req.dataLen; - // if the request is larger then a block, then request each block - // individually - MemBlock mb; // in case we need to allocate a temporary block.. - IORequest blockReq; // for requests we may need to make - blockReq.dataLen = _blockSize; - blockReq.data = nullptr; + // if the request is larger then a block, then request each block + // individually + MemBlock mb; // in case we need to allocate a temporary block.. + IORequest blockReq; // for requests we may need to make + blockReq.dataLen = _blockSize; + blockReq.data = nullptr; - unsigned char *out = req.data; - while (size != 0u) { - blockReq.offset = blockNum * _blockSize; + unsigned char *out = req.data; + while (size != 0u) { + blockReq.offset = blockNum * _blockSize; - // if we're reading a full block, then read directly into the - // result buffer instead of using a temporary - if (partialOffset == 0 && size >= (size_t)_blockSize) { - blockReq.data = out; - } else { - if (mb.data == nullptr) mb = MemoryPool::allocate(_blockSize); - blockReq.data = mb.data; - } - - ssize_t readSize = cacheReadOneBlock(blockReq); - if (readSize <= partialOffset) break; // didn't get enough bytes - - int cpySize = min((size_t)(readSize - partialOffset), size); - CHECK(cpySize <= readSize); - - // if we read to a temporary buffer, then move the data - if (blockReq.data != out) { - memcpy(out, blockReq.data + partialOffset, cpySize); - } - - result += cpySize; - size -= cpySize; - out += cpySize; - ++blockNum; - partialOffset = 0; - - if (readSize < _blockSize) break; + // if we're reading a full block, then read directly into the + // result buffer instead of using a temporary + if (partialOffset == 0 && size >= (size_t)_blockSize) { + blockReq.data = out; + } else { + if (mb.data == nullptr) mb = MemoryPool::allocate(_blockSize); + blockReq.data = mb.data; } - if (mb.data != nullptr) MemoryPool::release(mb); + ssize_t readSize = cacheReadOneBlock(blockReq); + if (readSize <= partialOffset) break; // didn't get enough bytes + + int cpySize = min((size_t)(readSize - partialOffset), size); + CHECK(cpySize <= readSize); + + // if we read to a temporary buffer, then move the data + if (blockReq.data != out) { + memcpy(out, blockReq.data + partialOffset, cpySize); + } + + result += cpySize; + size -= cpySize; + out += cpySize; + ++blockNum; + partialOffset = 0; + + if (readSize < _blockSize) break; } + if (mb.data != nullptr) MemoryPool::release(mb); + return result; } diff --git a/encfs/BlockNameIO.cpp b/encfs/BlockNameIO.cpp index 572ddf0..1f432a6 100644 --- a/encfs/BlockNameIO.cpp +++ b/encfs/BlockNameIO.cpp @@ -91,9 +91,8 @@ Interface BlockNameIO::CurrentInterface(bool caseInsensitive) { // implement major version 4 plus support for two prior versions if (caseInsensitive) { return Interface("nameio/block32", 4, 0, 2); - } else { - return Interface("nameio/block", 4, 0, 2); } + return Interface("nameio/block", 4, 0, 2); } BlockNameIO::BlockNameIO(const Interface &iface, std::shared_ptr cipher, @@ -121,9 +120,8 @@ int BlockNameIO::maxEncodedNameLen(int plaintextNameLen) const { int encodedNameLen = numBlocks * _bs + 2; // 2 checksum bytes if (_caseInsensitive) { return B256ToB32Bytes(encodedNameLen); - } else { - return B256ToB64Bytes(encodedNameLen); } + return B256ToB64Bytes(encodedNameLen); } int BlockNameIO::maxDecodedNameLen(int encodedNameLen) const { diff --git a/encfs/CipherFileIO.cpp b/encfs/CipherFileIO.cpp index 17e109f..079eae9 100644 --- a/encfs/CipherFileIO.cpp +++ b/encfs/CipherFileIO.cpp @@ -105,10 +105,9 @@ bool CipherFileIO::setIV(uint64_t iv) { // duh -- there are no file headers for directories! externalIV = iv; return base->setIV(iv); - } else { - VLOG(1) << "writeHeader failed to re-open for write"; - return false; } + VLOG(1) << "writeHeader failed to re-open for write"; + return false; } initHeader(); } @@ -391,9 +390,8 @@ bool CipherFileIO::blockWrite(unsigned char *buf, int size, VLOG(1) << "Called blockWrite"; if (!fsConfig->reverseEncryption) { return cipher->blockEncode(buf, size, _iv64, key); - } else { - return cipher->blockDecode(buf, size, _iv64, key); } + return cipher->blockDecode(buf, size, _iv64, key); } bool CipherFileIO::streamWrite(unsigned char *buf, int size, @@ -401,36 +399,32 @@ bool CipherFileIO::streamWrite(unsigned char *buf, int size, VLOG(1) << "Called streamWrite"; if (!fsConfig->reverseEncryption) { return cipher->streamEncode(buf, size, _iv64, key); - } else { - return cipher->streamDecode(buf, size, _iv64, key); } + return cipher->streamDecode(buf, size, _iv64, key); } bool CipherFileIO::blockRead(unsigned char *buf, int size, uint64_t _iv64) const { if (fsConfig->reverseEncryption) { return cipher->blockEncode(buf, size, _iv64, key); - } else { - if (_allowHoles) { - // special case - leave all 0's alone - for (int i = 0; i < size; ++i) { - if (buf[i] != 0) return cipher->blockDecode(buf, size, _iv64, key); - } - - return true; - } else { - return cipher->blockDecode(buf, size, _iv64, key); - } } + if (_allowHoles) { + // special case - leave all 0's alone + for (int i = 0; i < size; ++i) { + if (buf[i] != 0) return cipher->blockDecode(buf, size, _iv64, key); + } + + return true; + } + return cipher->blockDecode(buf, size, _iv64, key); } bool CipherFileIO::streamRead(unsigned char *buf, int size, uint64_t _iv64) const { if (fsConfig->reverseEncryption) { return cipher->streamEncode(buf, size, _iv64, key); - } else { - return cipher->streamDecode(buf, size, _iv64, key); } + return cipher->streamDecode(buf, size, _iv64, key); } int CipherFileIO::truncate(off_t size) { @@ -519,11 +513,10 @@ ssize_t CipherFileIO::read(const IORequest &origReq) const { VLOG(1) << "read " << readBytes << " bytes from backing file"; if (readBytes < 0) { return readBytes; // Return error code - } else { - ssize_t sum = headerBytes + readBytes; - VLOG(1) << "returning sum=" << sum; - return sum; } + ssize_t sum = headerBytes + readBytes; + VLOG(1) << "returning sum=" << sum; + return sum; } bool CipherFileIO::isWritable() const { return base->isWritable(); } diff --git a/encfs/ConfigReader.cpp b/encfs/ConfigReader.cpp index 9a1fbab..dfbf544 100644 --- a/encfs/ConfigReader.cpp +++ b/encfs/ConfigReader.cpp @@ -129,9 +129,8 @@ ConfigVar ConfigReader::operator[](const std::string &varName) const { auto it = vars.find(varName); if (it == vars.end()) { return ConfigVar(); - } else { - return it->second; } + return it->second; } ConfigVar &ConfigReader::operator[](const std::string &varName) { diff --git a/encfs/ConfigVar.cpp b/encfs/ConfigVar.cpp index dfa5e94..19ed71c 100644 --- a/encfs/ConfigVar.cpp +++ b/encfs/ConfigVar.cpp @@ -45,9 +45,8 @@ ConfigVar::~ConfigVar() { pd.reset(); } ConfigVar &ConfigVar::operator=(const ConfigVar &src) { if (src.pd == pd) { return *this; - } else { - pd = src.pd; } + pd = src.pd; return *this; } @@ -142,9 +141,8 @@ int ConfigVar::readInt(int defaultValue) const { if (offset >= bytes) { return defaultValue; - } else { - return readInt(); } + return readInt(); } bool ConfigVar::readBool(bool defaultValue) const { diff --git a/encfs/DirNode.cpp b/encfs/DirNode.cpp index 35bbaf7..1e82d76 100644 --- a/encfs/DirNode.cpp +++ b/encfs/DirNode.cpp @@ -81,10 +81,9 @@ static bool _nextName(struct dirent *&de, const std::shared_ptr &dir, } if (inode != nullptr) *inode = de->d_ino; return true; - } else { - if (fileType != nullptr) *fileType = 0; - return false; } + if (fileType != nullptr) *fileType = 0; + return false; } std::string DirTraverse::nextPlaintextName(int *fileType, ino_t *inode) { @@ -350,19 +349,18 @@ DirTraverse DirNode::openDir(const char *plaintextPath) { if (dir == nullptr) { VLOG(1) << "opendir error " << strerror(errno); return DirTraverse(shared_ptr(), 0, std::shared_ptr()); - } else { - std::shared_ptr dp(dir, DirDeleter()); - - uint64_t iv = 0; - // if we're using chained IV mode, then compute the IV at this - // directory level.. - try { - if (naming->getChainedNameIV()) naming->encodePath(plaintextPath, &iv); - } catch (encfs::Error &err) { - RLOG(ERROR) << "encode err: " << err.what(); - } - return DirTraverse(dp, iv, naming); } + std::shared_ptr dp(dir, DirDeleter()); + + uint64_t iv = 0; + // if we're using chained IV mode, then compute the IV at this + // directory level.. + try { + if (naming->getChainedNameIV()) naming->encodePath(plaintextPath, &iv); + } catch (encfs::Error &err) { + RLOG(ERROR) << "encode err: " << err.what(); + } + return DirTraverse(dp, iv, naming); } bool DirNode::genRenameList(list &renameList, const char *fromP, @@ -477,9 +475,8 @@ std::shared_ptr DirNode::newRenameOp(const char *fromP, if (!genRenameList(*renameList.get(), fromP, toP)) { RLOG(WARNING) << "Error during generation of recursive rename list"; return std::shared_ptr(); - } else { - return std::make_shared(this, renameList); } + return std::make_shared(this, renameList); } int DirNode::mkdir(const char *plaintextPath, mode_t mode, uid_t uid, @@ -679,9 +676,8 @@ std::shared_ptr DirNode::openNode(const char *plainName, if (node && (*result = node->open(flags)) >= 0) { return node; - } else { - return std::shared_ptr(); } + return std::shared_ptr(); } int DirNode::unlink(const char *plaintextName) { diff --git a/encfs/FileNode.cpp b/encfs/FileNode.cpp index 81d87f2..0aa3d51 100644 --- a/encfs/FileNode.cpp +++ b/encfs/FileNode.cpp @@ -102,9 +102,8 @@ static bool setIV(const std::shared_ptr &io, uint64_t iv) { struct stat stbuf; if ((io->getAttr(&stbuf) < 0) || S_ISREG(stbuf.st_mode)) { return io->setIV(iv); - } else { - return true; } + return true; } bool FileNode::setName(const char *plaintextName_, const char *cipherName_, @@ -261,9 +260,8 @@ int FileNode::sync(bool datasync) { if (res == -1) res = -errno; return res; - } else { - return fh; } + return fh; } } // namespace encfs diff --git a/encfs/FileUtils.cpp b/encfs/FileUtils.cpp index 5047928..bffac1a 100644 --- a/encfs/FileUtils.cpp +++ b/encfs/FileUtils.cpp @@ -127,27 +127,24 @@ bool fileExists(const char *fileName) { struct stat buf; if (lstat(fileName, &buf) == 0) { return true; - } else { - // XXX show perror? - return false; } + // XXX show perror? + return false; } bool isDirectory(const char *fileName) { struct stat buf; if (lstat(fileName, &buf) == 0) { return S_ISDIR(buf.st_mode); - } else { - return false; } + return false; } bool isAbsolutePath(const char *fileName) { if ((fileName != nullptr) && fileName[0] != '\0' && fileName[0] == '/') { return true; - } else { - return false; } + return false; } const char *lastPathElement(const char *name) { @@ -159,9 +156,8 @@ std::string parentDirectory(const std::string &path) { size_t last = path.find_last_of('/'); if (last == string::npos) { return string(""); - } else { - return path.substr(0, last); } + return path.substr(0, last); } bool userAllowMkdir(const char *path, mode_t mode) { @@ -196,14 +192,12 @@ bool userAllowMkdir(int promptno, const char *path, mode_t mode) { if (result < 0) { perror(_("Unable to create directory: ")); return false; - } else { - return true; } - } else { - // Directory not created, by user request - cerr << _("Directory not created.") << "\n"; - return false; + return true; } + // Directory not created, by user request + cerr << _("Directory not created.") << "\n"; + return false; } /** @@ -842,10 +836,12 @@ static bool boolDefault(const char *prompt, bool defaultValue) { if (cin.fail() || response == "") { value = defaultValue; break; - } else if (response == "y") { + } + if (response == "y") { value = true; break; - } else if (response == "n") { + } + if (response == "n") { value = false; break; } @@ -1119,10 +1115,9 @@ RootPtr createV6Config(EncFS_Context *ctx, _("Unable to instanciate cipher %s, key size %i, block size %i"), alg.name.c_str(), keySize, blockSize); return rootInfo; - } else { - VLOG(1) << "Using cipher " << alg.name << ", key size " << keySize - << ", block size " << blockSize; } + VLOG(1) << "Using cipher " << alg.name << ", key size " << keySize + << ", block size " << blockSize; std::shared_ptr config(new EncFSConfig); @@ -1672,10 +1667,9 @@ int remountFS(EncFS_Context *ctx) { if (rootInfo) { ctx->setRoot(rootInfo->root); return 0; - } else { - RLOG(WARNING) << "Remount failed"; - return -EACCES; } + RLOG(WARNING) << "Remount failed"; + return -EACCES; } } // namespace encfs diff --git a/encfs/Interface.cpp b/encfs/Interface.cpp index 4f996e6..0d7a563 100644 --- a/encfs/Interface.cpp +++ b/encfs/Interface.cpp @@ -85,11 +85,11 @@ static int sign( int a, int b ) static int sign(int a, int b) { if (a < b) { return 0; - } else if (a == b) { - return 1; - } else { - return 2; } + if (a == b) { + return 1; + } + return 2; } #endif @@ -117,33 +117,29 @@ bool Interface::implements(const Interface &B) const { bool operator<(const Interface &A, const Interface &B) { if (A.name() == B.name()) { return (diffSum(A, B) < EqualVersion); - } else { - return A.name() < B.name(); } + return A.name() < B.name(); } bool operator>(const Interface &A, const Interface &B) { if (A.name() == B.name()) { return (diffSum(A, B) > EqualVersion); - } else { - return A.name() < B.name(); } + return A.name() < B.name(); } bool operator<=(const Interface &A, const Interface &B) { if (A.name() == B.name()) { return (diffSum(A, B) <= EqualVersion); - } else { - return A.name() < B.name(); } + return A.name() < B.name(); } bool operator>=(const Interface &A, const Interface &B) { if (A.name() == B.name()) { return (diffSum(A, B) >= EqualVersion); - } else { - return A.name() < B.name(); } + return A.name() < B.name(); } ConfigVar &operator<<(ConfigVar &dst, const Interface &iface) { diff --git a/encfs/RawFileIO.cpp b/encfs/RawFileIO.cpp index e9a5c4b..845ab8b 100644 --- a/encfs/RawFileIO.cpp +++ b/encfs/RawFileIO.cpp @@ -185,13 +185,11 @@ off_t RawFileIO::getSize() const { const_cast(this)->fileSize = stbuf.st_size; const_cast(this)->knownSize = true; return fileSize; - } else { - RLOG(ERROR) << "getSize on " << name << " failed: " << strerror(errno); - return -1; } - } else { - return fileSize; + RLOG(ERROR) << "getSize on " << name << " failed: " << strerror(errno); + return -1; } + return fileSize; } ssize_t RawFileIO::read(const IORequest &req) const { @@ -237,14 +235,13 @@ bool RawFileIO::write(const IORequest &req) { << req.dataLen << ", max retries reached"; knownSize = false; return false; - } else { - if (knownSize) { - off_t last = req.offset + req.dataLen; - if (last > fileSize) fileSize = last; - } - - return true; } + if (knownSize) { + off_t last = req.offset + req.dataLen; + if (last > fileSize) fileSize = last; + } + + return true; } int RawFileIO::truncate(off_t size) { diff --git a/encfs/SSL_Cipher.cpp b/encfs/SSL_Cipher.cpp index 62fd6f2..b23da1a 100644 --- a/encfs/SSL_Cipher.cpp +++ b/encfs/SSL_Cipher.cpp @@ -368,9 +368,9 @@ CipherKey SSL_Cipher::newKey(const char *password, int passwdLength, if (res <= 0) { RLOG(WARNING) << "openssl error, PBKDF2 failed"; return CipherKey(); - } else { - iterationCount = res; } + iterationCount = res; + } else { // known iteration length if (PKCS5_PBKDF2_HMAC_SHA1( @@ -510,9 +510,8 @@ bool SSL_Cipher::randomize(unsigned char *buf, int len, } return false; - } else { - return true; } + return true; } uint64_t SSL_Cipher::MAC_64(const unsigned char *data, int len, @@ -599,9 +598,8 @@ bool SSL_Cipher::compareKey(const CipherKey &A, const CipherKey &B) const { if (memcmp(key1->buffer, key2->buffer, _keySize + _ivLength) != 0) { return false; - } else { - return true; } + return true; } int SSL_Cipher::encodedKeySize() const { diff --git a/encfs/XmlReader.cpp b/encfs/XmlReader.cpp index de0dfa5..e914e2f 100644 --- a/encfs/XmlReader.cpp +++ b/encfs/XmlReader.cpp @@ -146,17 +146,14 @@ class XmlNode : virtual public XmlValue { const char *value = element->Attribute(name + 1); if (value != nullptr) { return std::make_shared(value); - } else { - return XmlValuePtr(); - } - } else { - const tinyxml2::XMLElement *el = element->FirstChildElement(name); - if (el != nullptr) { - return XmlValuePtr(new XmlNode(el)); - } else { - return XmlValuePtr(); } + return XmlValuePtr(); } + const tinyxml2::XMLElement *el = element->FirstChildElement(name); + if (el != nullptr) { + return XmlValuePtr(new XmlNode(el)); + } + return XmlValuePtr(); } }; diff --git a/encfs/autosprintf.cpp b/encfs/autosprintf.cpp index 6654b1c..33bbe32 100644 --- a/encfs/autosprintf.cpp +++ b/encfs/autosprintf.cpp @@ -57,9 +57,8 @@ autosprintf::operator char *() const { auto *copy = new char[length]; memcpy(copy, str, length); return copy; - } else { - return nullptr; } + return nullptr; } autosprintf::operator std::string() const { return std::string(str != nullptr ? str : "(error in autosprintf)"); diff --git a/encfs/encfs.cpp b/encfs/encfs.cpp index c3ec08c..816125d 100644 --- a/encfs/encfs.cpp +++ b/encfs/encfs.cpp @@ -397,10 +397,9 @@ int _do_readlink(EncFS_Context *ctx, const string &cyName, char *buf, buf[size - 1] = '\0'; return ESUCCESS; - } else { - RLOG(WARNING) << "Error decoding link"; - return -1; } + RLOG(WARNING) << "Error decoding link"; + return -1; } int encfs_readlink(const char *path, char *buf, size_t size) { @@ -655,9 +654,8 @@ int encfs_fsync(const char *path, int dataSync, struct fuse_file_info *file) { int _do_write(FileNode *fnode, unsigned char *ptr, size_t size, off_t offset) { if (fnode->write(offset, ptr, size)) { return size; - } else { - return -EIO; } + return -EIO; } int encfs_write(const char *path, const char *buf, size_t size, off_t offset, diff --git a/encfs/main.cpp b/encfs/main.cpp index 98bcf11..e22168e 100644 --- a/encfs/main.cpp +++ b/encfs/main.cpp @@ -785,15 +785,14 @@ static bool unmountFS(EncFS_Context *ctx) { ctx->setRoot(std::shared_ptr()); return false; - } else { + } // Time to unmount! #if FUSE_USE_VERSION < 30 - fuse_unmount(arg->opts->mountPoint.c_str(), nullptr); + fuse_unmount(arg->opts->mountPoint.c_str(), nullptr); #else - fuse_unmount(fuse_get_context()->fuse); + fuse_unmount(fuse_get_context()->fuse); #endif - // fuse_unmount succeeds and returns void - RLOG(INFO) << "Filesystem inactive, unmounted: " << arg->opts->mountPoint; - return true; - } + // fuse_unmount succeeds and returns void + RLOG(INFO) << "Filesystem inactive, unmounted: " << arg->opts->mountPoint; + return true; } From d7852e6f56fa5c621709503618bc23fc412e2b18 Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Thu, 3 Aug 2017 22:46:34 -0700 Subject: [PATCH 08/10] modernize: braces around statements --- encfs/BlockFileIO.cpp | 64 +++++++++++++----- encfs/BlockNameIO.cpp | 20 ++++-- encfs/Cipher.cpp | 8 ++- encfs/CipherFileIO.cpp | 28 ++++++-- encfs/ConfigReader.cpp | 8 ++- encfs/ConfigVar.cpp | 8 ++- encfs/Context.cpp | 4 +- encfs/DirNode.cpp | 52 +++++++++++---- encfs/FileNode.cpp | 32 ++++++--- encfs/FileUtils.cpp | 48 ++++++++++---- encfs/Interface.cpp | 4 +- encfs/MACFileIO.cpp | 12 +++- encfs/MemoryPool.cpp | 4 +- encfs/NameIO.cpp | 16 +++-- encfs/RawFileIO.cpp | 16 +++-- encfs/SSL_Cipher.cpp | 40 +++++++++--- encfs/StreamNameIO.cpp | 12 +++- encfs/XmlReader.cpp | 40 +++++++++--- encfs/autosprintf.cpp | 4 +- encfs/base64.cpp | 8 ++- encfs/encfs.cpp | 136 +++++++++++++++++++++++++++++---------- encfs/main.cpp | 60 ++++++++++++----- encfs/openssl.cpp | 4 +- encfs/readpassphrase.cpp | 24 +++++-- 24 files changed, 489 insertions(+), 163 deletions(-) diff --git a/encfs/BlockFileIO.cpp b/encfs/BlockFileIO.cpp index 0ce9501..ee9a835 100644 --- a/encfs/BlockFileIO.cpp +++ b/encfs/BlockFileIO.cpp @@ -70,11 +70,15 @@ ssize_t BlockFileIO::cacheReadOneBlock(const IORequest &req) const { if ((!_noCache) && (req.offset == _cache.offset) && (_cache.dataLen != 0)) { // satisfy request from cache int len = req.dataLen; - if (_cache.dataLen < len) len = _cache.dataLen; // Don't read past EOF + if (_cache.dataLen < len) { + len = _cache.dataLen; // Don't read past EOF + } memcpy(req.data, _cache.data, len); return len; } - if (_cache.dataLen > 0) clearCache(_cache, _blockSize); + if (_cache.dataLen > 0) { + clearCache(_cache, _blockSize); + } // cache results of read -- issue reads for full blocks IORequest tmp; @@ -100,7 +104,9 @@ bool BlockFileIO::cacheWriteOneBlock(const IORequest &req) { _cache.offset = req.offset; _cache.dataLen = req.dataLen; bool ok = writeOneBlock(req); - if (!ok) clearCache(_cache, _blockSize); + if (!ok) { + clearCache(_cache, _blockSize); + } return ok; } @@ -141,12 +147,16 @@ ssize_t BlockFileIO::read(const IORequest &req) const { if (partialOffset == 0 && size >= (size_t)_blockSize) { blockReq.data = out; } else { - if (mb.data == nullptr) mb = MemoryPool::allocate(_blockSize); + if (mb.data == nullptr) { + mb = MemoryPool::allocate(_blockSize); + } blockReq.data = mb.data; } ssize_t readSize = cacheReadOneBlock(blockReq); - if (readSize <= partialOffset) break; // didn't get enough bytes + if (readSize <= partialOffset) { + break; // didn't get enough bytes + } int cpySize = min((size_t)(readSize - partialOffset), size); CHECK(cpySize <= readSize); @@ -162,10 +172,14 @@ ssize_t BlockFileIO::read(const IORequest &req) const { ++blockNum; partialOffset = 0; - if (readSize < _blockSize) break; + if (readSize < _blockSize) { + break; + } } - if (mb.data != nullptr) MemoryPool::release(mb); + if (mb.data != nullptr) { + MemoryPool::release(mb); + } return result; } @@ -174,7 +188,9 @@ bool BlockFileIO::write(const IORequest &req) { CHECK(_blockSize != 0); off_t fileSize = getSize(); - if (fileSize < 0) return false; + if (fileSize < 0) { + return false; + } // where write request begins off_t blockNum = req.offset / _blockSize; @@ -185,7 +201,9 @@ bool BlockFileIO::write(const IORequest &req) { ssize_t lastBlockSize = fileSize % _blockSize; off_t lastNonEmptyBlock = lastFileBlock; - if (lastBlockSize == 0) --lastNonEmptyBlock; + if (lastBlockSize == 0) { + --lastNonEmptyBlock; + } if (req.offset > fileSize) { // extend file first to fill hole with 0's.. @@ -197,7 +215,9 @@ bool BlockFileIO::write(const IORequest &req) { // request as-is.. if (partialOffset == 0 && req.dataLen <= _blockSize) { // if writing a full block.. pretty safe.. - if (req.dataLen == _blockSize) return cacheWriteOneBlock(req); + if (req.dataLen == _blockSize) { + return cacheWriteOneBlock(req); + } // if writing a partial block, but at least as much as what is // already there.. @@ -230,7 +250,9 @@ bool BlockFileIO::write(const IORequest &req) { } else { // need a temporary buffer, since we have to either merge or pad // the data. - if (mb.data == nullptr) mb = MemoryPool::allocate(_blockSize); + if (mb.data == nullptr) { + mb = MemoryPool::allocate(_blockSize); + } memset(mb.data, 0, _blockSize); blockReq.data = mb.data; @@ -264,7 +286,9 @@ bool BlockFileIO::write(const IORequest &req) { partialOffset = 0; } - if (mb.data != nullptr) MemoryPool::release(mb); + if (mb.data != nullptr) { + MemoryPool::release(mb); + } return ok; } @@ -340,7 +364,9 @@ void BlockFileIO::padFile(off_t oldSize, off_t newSize, bool forceWrite) { } } - if (mb.data != nullptr) MemoryPool::release(mb); + if (mb.data != nullptr) { + MemoryPool::release(mb); + } } int BlockFileIO::truncateBase(off_t size, FileIO *base) { @@ -354,7 +380,9 @@ int BlockFileIO::truncateBase(off_t size, FileIO *base) { // states that it will pad with 0's. // do the truncate so that the underlying filesystem can allocate // the space, and then we'll fill it in padFile.. - if (base != nullptr) base->truncate(size); + if (base != nullptr) { + base->truncate(size); + } const bool forceWrite = true; padFile(oldSize, size, forceWrite); @@ -375,7 +403,9 @@ int BlockFileIO::truncateBase(off_t size, FileIO *base) { ssize_t rdSz = cacheReadOneBlock(req); // do the truncate - if (base != nullptr) res = base->truncate(size); + if (base != nullptr) { + res = base->truncate(size); + } // write back out partial block req.dataLen = partialBlock; @@ -391,7 +421,9 @@ int BlockFileIO::truncateBase(off_t size, FileIO *base) { } else { // truncating on a block bounday. No need to re-encode the last // block.. - if (base != nullptr) res = base->truncate(size); + if (base != nullptr) { + res = base->truncate(size); + } } return res; diff --git a/encfs/BlockNameIO.cpp b/encfs/BlockNameIO.cpp index 1f432a6..3a25016 100644 --- a/encfs/BlockNameIO.cpp +++ b/encfs/BlockNameIO.cpp @@ -39,7 +39,9 @@ static std::shared_ptr NewBlockNameIO( const Interface &iface, const std::shared_ptr &cipher, const CipherKey &key) { int blockSize = 8; - if (cipher) blockSize = cipher->cipherBlockSize(); + if (cipher) { + blockSize = cipher->cipherBlockSize(); + } return std::shared_ptr( new BlockNameIO(iface, cipher, key, blockSize, false)); @@ -49,7 +51,9 @@ static std::shared_ptr NewBlockNameIO32( const Interface &iface, const std::shared_ptr &cipher, const CipherKey &key) { int blockSize = 8; - if (cipher) blockSize = cipher->cipherBlockSize(); + if (cipher) { + blockSize = cipher->cipherBlockSize(); + } return std::shared_ptr( new BlockNameIO(iface, cipher, key, blockSize, true)); @@ -135,7 +139,9 @@ int BlockNameIO::encodeName(const char *plaintextName, int length, uint64_t *iv, // Pad encryption buffer to block boundary.. int padding = _bs - length % _bs; - if (padding == 0) padding = _bs; // padding a full extra block! + if (padding == 0) { + padding = _bs; // padding a full extra block! + } rAssert(bufferLength >= length + 2 + padding); memset(encodedName + length + 2, (unsigned char)padding, padding); @@ -145,7 +151,9 @@ int BlockNameIO::encodeName(const char *plaintextName, int length, uint64_t *iv, // store the IV before it is modified by the MAC call. uint64_t tmpIV = 0; - if ((iv != nullptr) && _interface >= 3) tmpIV = *iv; + if ((iv != nullptr) && _interface >= 3) { + tmpIV = *iv; + } // include padding in MAC computation unsigned int mac = _cipher->MAC_16((unsigned char *)encodedName + 2, @@ -207,7 +215,9 @@ int BlockNameIO::decodeName(const char *encodedName, int length, uint64_t *iv, ((unsigned int)((unsigned char)tmpBuf[1])); uint64_t tmpIV = 0; - if ((iv != nullptr) && _interface >= 3) tmpIV = *iv; + if ((iv != nullptr) && _interface >= 3) { + tmpIV = *iv; + } _cipher->blockDecode((unsigned char *)tmpBuf + 2, decodedStreamLen, (uint64_t)mac ^ tmpIV, _key); diff --git a/encfs/Cipher.cpp b/encfs/Cipher.cpp index 1791459..bbe43cf 100644 --- a/encfs/Cipher.cpp +++ b/encfs/Cipher.cpp @@ -65,7 +65,9 @@ std::list Cipher::GetAlgorithmList( list result; - if (gCipherMap == nullptr) return result; + if (gCipherMap == nullptr) { + return result; + } CipherMap_t::const_iterator it; CipherMap_t::const_iterator mapEnd = gCipherMap->end(); @@ -98,7 +100,9 @@ bool Cipher::Register(const char *name, const char *description, const Interface &iface, const Range &keyLength, const Range &blockSize, CipherConstructor fn, bool hidden) { - if (gCipherMap == nullptr) gCipherMap = new CipherMap_t; + if (gCipherMap == nullptr) { + gCipherMap = new CipherMap_t; + } CipherAlg ca; ca.hidden = hidden; diff --git a/encfs/CipherFileIO.cpp b/encfs/CipherFileIO.cpp index 079eae9..e825b6e 100644 --- a/encfs/CipherFileIO.cpp +++ b/encfs/CipherFileIO.cpp @@ -71,7 +71,9 @@ Interface CipherFileIO::interface() const { return CipherFileIO_iface; } int CipherFileIO::open(int flags) { int res = base->open(flags); - if (res >= 0) lastFlags = flags; + if (res >= 0) { + lastFlags = flags; + } return res; } @@ -188,7 +190,9 @@ void CipherFileIO::initHeader() { cipher->streamDecode(buf, sizeof(buf), externalIV, key); fileIV = 0; - for (int i = 0; i < 8; ++i) fileIV = (fileIV << 8) | (uint64_t)buf[i]; + for (int i = 0; i < 8; ++i) { + fileIV = (fileIV << 8) | (uint64_t)buf[i]; + } rAssert(fileIV != 0); // 0 is never used.. } else { @@ -201,7 +205,9 @@ void CipherFileIO::initHeader() { } fileIV = 0; - for (int i = 0; i < 8; ++i) fileIV = (fileIV << 8) | (uint64_t)buf[i]; + for (int i = 0; i < 8; ++i) { + fileIV = (fileIV << 8) | (uint64_t)buf[i]; + } if (fileIV == 0) { RLOG(WARNING) << "Unexpected result: randomize returned 8 null bytes!"; @@ -360,7 +366,9 @@ bool CipherFileIO::writeOneBlock(const IORequest &req) { int bs = blockSize(); off_t blockNum = req.offset / bs; - if (haveHeader && fileIV == 0) initHeader(); + if (haveHeader && fileIV == 0) { + initHeader(); + } bool ok; if (req.dataLen != bs) { @@ -411,7 +419,9 @@ bool CipherFileIO::blockRead(unsigned char *buf, int size, if (_allowHoles) { // special case - leave all 0's alone for (int i = 0; i < size; ++i) { - if (buf[i] != 0) return cipher->blockDecode(buf, size, _iv64, key); + if (buf[i] != 0) { + return cipher->blockDecode(buf, size, _iv64, key); + } } return true; @@ -448,7 +458,9 @@ int CipherFileIO::truncate(off_t size) { // the wrong size.. res = BlockFileIO::truncateBase(size, nullptr); - if (res == 0) base->truncate(size + HEADER_SIZE); + if (res == 0) { + base->truncate(size + HEADER_SIZE); + } } return res; } @@ -497,7 +509,9 @@ ssize_t CipherFileIO::read(const IORequest &origReq) const { memcpy(req.data, &headerBuf[headerOffset], headerBytes); // the read does not want data beyond the header - if (headerBytes == req.dataLen) return headerBytes; + if (headerBytes == req.dataLen) { + return headerBytes; + } /* The rest of the request will be read from the backing file. * As we have already generated n=headerBytes bytes, the request is diff --git a/encfs/ConfigReader.cpp b/encfs/ConfigReader.cpp index dfbf544..8e2fd1a 100644 --- a/encfs/ConfigReader.cpp +++ b/encfs/ConfigReader.cpp @@ -42,12 +42,16 @@ ConfigReader::~ConfigReader() = default; bool ConfigReader::load(const char *fileName) { struct stat stbuf; memset(&stbuf, 0, sizeof(struct stat)); - if (lstat(fileName, &stbuf) != 0) return false; + if (lstat(fileName, &stbuf) != 0) { + return false; + } int size = stbuf.st_size; int fd = open(fileName, O_RDONLY); - if (fd < 0) return false; + if (fd < 0) { + return false; + } auto *buf = new char[size]; diff --git a/encfs/ConfigVar.cpp b/encfs/ConfigVar.cpp index 19ed71c..b9dceef 100644 --- a/encfs/ConfigVar.cpp +++ b/encfs/ConfigVar.cpp @@ -56,7 +56,9 @@ void ConfigVar::resetOffset() { pd->offset = 0; } int ConfigVar::read(unsigned char *buffer_, int bytes) const { int toCopy = MIN(bytes, pd->buffer.size() - pd->offset); - if (toCopy > 0) memcpy(buffer_, pd->buffer.data() + pd->offset, toCopy); + if (toCopy > 0) { + memcpy(buffer_, pd->buffer.data() + pd->offset, toCopy); + } pd->offset += toCopy; @@ -106,7 +108,9 @@ void ConfigVar::writeInt(int val) { // find the starting point - we only need to output starting at the most // significant non-zero digit.. int start = 0; - while (digit[start] == 0x80) ++start; + while (digit[start] == 0x80) { + ++start; + } write(digit + start, 5 - start); } diff --git a/encfs/Context.cpp b/encfs/Context.cpp index c6bf276..16ec724 100644 --- a/encfs/Context.cpp +++ b/encfs/Context.cpp @@ -70,7 +70,9 @@ void EncFS_Context::setRoot(const std::shared_ptr &r) { Lock lock(contextMutex); root = r; - if (r) rootCipherDir = r->rootDirectory(); + if (r) { + rootCipherDir = r->rootDirectory(); + } } bool EncFS_Context::isMounted() { return root.get() != nullptr; } diff --git a/encfs/DirNode.cpp b/encfs/DirNode.cpp index 1e82d76..1c0f78c 100644 --- a/encfs/DirNode.cpp +++ b/encfs/DirNode.cpp @@ -79,10 +79,14 @@ static bool _nextName(struct dirent *&de, const std::shared_ptr &dir, *fileType = 0; #endif } - if (inode != nullptr) *inode = de->d_ino; + if (inode != nullptr) { + *inode = de->d_ino; + } return true; } - if (fileType != nullptr) *fileType = 0; + if (fileType != nullptr) { + *fileType = 0; + } return false; } @@ -356,7 +360,9 @@ DirTraverse DirNode::openDir(const char *plaintextPath) { // if we're using chained IV mode, then compute the IV at this // directory level.. try { - if (naming->getChainedNameIV()) naming->encodePath(plaintextPath, &iv); + if (naming->getChainedNameIV()) { + naming->encodePath(plaintextPath, &iv); + } } catch (encfs::Error &err) { RLOG(ERROR) << "encode err: " << err.what(); } @@ -375,13 +381,17 @@ bool DirNode::genRenameList(list &renameList, const char *fromP, string sourcePath = rootDir + fromCPart; // ok..... we wish it was so simple.. should almost never happen - if (fromIV == toIV) return true; + if (fromIV == toIV) { + return true; + } // generate the real destination path, where we expect to find the files.. VLOG(1) << "opendir " << sourcePath; std::shared_ptr dir = std::shared_ptr(opendir(sourcePath.c_str()), DirDeleter()); - if (!dir) return false; + if (!dir) { + return false; + } struct dirent *de = nullptr; while ((de = ::readdir(dir.get())) != nullptr) { @@ -489,13 +499,21 @@ int DirNode::mkdir(const char *plaintextPath, mode_t mode, uid_t uid, // if uid or gid are set, then that should be the directory owner int olduid = -1; int oldgid = -1; - if (uid != 0) olduid = setfsuid(uid); - if (gid != 0) oldgid = setfsgid(gid); + if (uid != 0) { + olduid = setfsuid(uid); + } + if (gid != 0) { + oldgid = setfsgid(gid); + } int res = ::mkdir(cyName.c_str(), mode); - if (olduid >= 0) setfsuid(olduid); - if (oldgid >= 0) setfsgid(oldgid); + if (olduid >= 0) { + setfsuid(olduid); + } + if (oldgid >= 0) { + setfsgid(oldgid); + } if (res == -1) { int eno = errno; @@ -527,7 +545,9 @@ int DirNode::rename(const char *fromPlaintext, const char *toPlaintext) { renameOp = newRenameOp(fromPlaintext, toPlaintext); if (!renameOp || !renameOp->apply()) { - if (renameOp) renameOp->undo(); + if (renameOp) { + renameOp->undo(); + } RLOG(WARNING) << "rename aborted"; return -EACCES; @@ -548,7 +568,9 @@ int DirNode::rename(const char *fromPlaintext, const char *toPlaintext) { res = -errno; renameNode(toPlaintext, fromPlaintext, false); - if (renameOp) renameOp->undo(); + if (renameOp) { + renameOp->undo(); + } } else if (preserve_mtime) { struct utimbuf ut; ut.actime = st.st_atime; @@ -616,7 +638,9 @@ std::shared_ptr DirNode::renameNode(const char *from, const char *to, << cname; if (node->setName(to, cname.c_str(), newIV, forwardMode)) { - if (ctx != nullptr) ctx->renameNode(from, to); + if (ctx != nullptr) { + ctx->renameNode(from, to); + } } else { // rename error! - put it back RLOG(ERROR) << "renameNode failed"; @@ -633,7 +657,9 @@ std::shared_ptr DirNode::findOrCreate(const char *plainName) { std::shared_ptr node; // See if we already have a FileNode for this path. - if (ctx != nullptr) node = ctx->lookupNode(plainName); + if (ctx != nullptr) { + node = ctx->lookupNode(plainName); + } // If we don't, create a new one. if (!node) { diff --git a/encfs/FileNode.cpp b/encfs/FileNode.cpp index 0aa3d51..4ae65a7 100644 --- a/encfs/FileNode.cpp +++ b/encfs/FileNode.cpp @@ -109,13 +109,19 @@ static bool setIV(const std::shared_ptr &io, uint64_t iv) { bool FileNode::setName(const char *plaintextName_, const char *cipherName_, uint64_t iv, bool setIVFirst) { // Lock _lock( mutex ); - if (cipherName_ != nullptr) VLOG(1) << "calling setIV on " << cipherName_; + if (cipherName_ != nullptr) { + VLOG(1) << "calling setIV on " << cipherName_; + } if (setIVFirst) { - if (fsConfig->config->externalIVChaining && !setIV(io, iv)) return false; + if (fsConfig->config->externalIVChaining && !setIV(io, iv)) { + return false; + } // now change the name.. - if (plaintextName_ != nullptr) this->_pname = plaintextName_; + if (plaintextName_ != nullptr) { + this->_pname = plaintextName_; + } if (cipherName_ != nullptr) { this->_cname = cipherName_; io->setFileName(cipherName_); @@ -124,7 +130,9 @@ bool FileNode::setName(const char *plaintextName_, const char *cipherName_, std::string oldPName = _pname; std::string oldCName = _cname; - if (plaintextName_ != nullptr) this->_pname = plaintextName_; + if (plaintextName_ != nullptr) { + this->_pname = plaintextName_; + } if (cipherName_ != nullptr) { this->_cname = cipherName_; io->setFileName(cipherName_); @@ -168,15 +176,21 @@ int FileNode::mknod(mode_t mode, dev_t rdev, uid_t uid, gid_t gid) { */ if (S_ISREG(mode)) { res = ::open(_cname.c_str(), O_CREAT | O_EXCL | O_WRONLY, mode); - if (res >= 0) res = ::close(res); + if (res >= 0) { + res = ::close(res); + } } else if (S_ISFIFO(mode)) { res = ::mkfifo(_cname.c_str(), mode); } else { res = ::mknod(_cname.c_str(), mode, rdev); } - if (olduid >= 0) setfsuid(olduid); - if (oldgid >= 0) setfsgid(oldgid); + if (olduid >= 0) { + setfsuid(olduid); + } + if (oldgid >= 0) { + setfsgid(oldgid); + } if (res == -1) { int eno = errno; @@ -257,7 +271,9 @@ int FileNode::sync(bool datasync) { res = fsync(fh); #endif - if (res == -1) res = -errno; + if (res == -1) { + res = -errno; + } return res; } diff --git a/encfs/FileUtils.cpp b/encfs/FileUtils.cpp index bffac1a..4e67ac9 100644 --- a/encfs/FileUtils.cpp +++ b/encfs/FileUtils.cpp @@ -450,7 +450,9 @@ bool saveConfig(ConfigType type, const string &rootDir, if (nm->environmentOverride != nullptr) { // use environment file if specified.. const char *envFile = getenv(nm->environmentOverride); - if (envFile != nullptr) path.assign(envFile); + if (envFile != nullptr) { + path.assign(envFile); + } } try { @@ -739,13 +741,17 @@ static int selectKeySize(const Cipher::CipherAlgorithm &alg) { if (numAvail < 5) { // show them all for (int i = 0; i <= numAvail; ++i) { - if (i != 0) cout << ", "; + if (i != 0) { + cout << ", "; + } cout << alg.keyLength.min() + i * alg.keyLength.inc(); } } else { // partial for (int i = 0; i < 3; ++i) { - if (i != 0) cout << ", "; + if (i != 0) { + cout << ", "; + } cout << alg.keyLength.min() + i * alg.keyLength.inc(); } cout << " ... " << alg.keyLength.max() - alg.keyLength.inc(); @@ -902,8 +908,12 @@ static void selectBlockMAC(int *macBytes, int *macRandBytes, bool forceMac) { cout << "\n"; randSize = (res == nullptr ? 0 : atoi(answer)); - if (randSize < 0) randSize = 0; - if (randSize > 8) randSize = 8; + if (randSize < 0) { + randSize = 0; + } + if (randSize > 8) { + randSize = 8; + } *macRandBytes = randSize; } @@ -984,7 +994,9 @@ RootPtr createV6Config(EncFS_Context *ctx, " anything else, or an empty line will select standard mode.\n" "?> "); - if (annotate) cerr << "$PROMPT$ config_option" << endl; + if (annotate) { + cerr << "$PROMPT$ config_option" << endl; + } char *res = fgets(answer, sizeof(answer), stdin); (void)res; @@ -1063,7 +1075,9 @@ RootPtr createV6Config(EncFS_Context *ctx, /* Reverse mounts are read-only by default (set in main.cpp). * If uniqueIV is off, writing can be allowed, because there * is no header that could be overwritten */ - if (!uniqueIV) opts->readOnly = false; + if (!uniqueIV) { + opts->readOnly = false; + } } } @@ -1091,7 +1105,9 @@ RootPtr createV6Config(EncFS_Context *ctx, /* Reverse mounts are read-only by default (set in main.cpp). * If uniqueIV is off, writing can be allowed, because there * is no header that could be overwritten */ - if (!uniqueIV) opts->readOnly = false; + if (!uniqueIV) { + opts->readOnly = false; + } } else { chainedIV = selectChainedIV(); uniqueIV = selectUniqueIV(true); @@ -1175,7 +1191,9 @@ RootPtr createV6Config(EncFS_Context *ctx, CipherKey userKey; VLOG(1) << "useStdin: " << useStdin; if (useStdin) { - if (annotate) cerr << "$PROMPT$ new_passwd" << endl; + if (annotate) { + cerr << "$PROMPT$ new_passwd" << endl; + } userKey = config->getUserKey(useStdin); } else if (!passwordProgram.empty()) { userKey = config->getUserKey(passwordProgram, rootDir); @@ -1573,7 +1591,9 @@ RootPtr initFS(EncFS_Context *ctx, const std::shared_ptr &opts) { /* Reverse mounts are read-only by default (set in main.cpp). * If uniqueIV is off, writing can be allowed, because there * is no header that could be overwritten */ - if (!config->uniqueIV) opts->readOnly = false; + if (!config->uniqueIV) { + opts->readOnly = false; + } } // first, instanciate the cipher. @@ -1600,13 +1620,17 @@ RootPtr initFS(EncFS_Context *ctx, const std::shared_ptr &opts) { if (opts->passwordProgram.empty()) { VLOG(1) << "useStdin: " << opts->useStdin; - if (opts->annotate) cerr << "$PROMPT$ passwd" << endl; + if (opts->annotate) { + cerr << "$PROMPT$ passwd" << endl; + } userKey = config->getUserKey(opts->useStdin); } else { userKey = config->getUserKey(opts->passwordProgram, opts->rootDir); } - if (!userKey) return rootInfo; + if (!userKey) { + return rootInfo; + } VLOG(1) << "cipher key size = " << cipher->encodedKeySize(); // decode volume key.. diff --git a/encfs/Interface.cpp b/encfs/Interface.cpp index 0d7a563..86111ab 100644 --- a/encfs/Interface.cpp +++ b/encfs/Interface.cpp @@ -108,7 +108,9 @@ bool Interface::implements(const Interface &B) const { << ":" << age() << ") implements " << B.name() << "(" << B.current() << ":" << B.revision() << ")"; - if (name() != B.name()) return false; + if (name() != B.name()) { + return false; + } int currentDiff = current() - B.current(); return (currentDiff >= 0 && currentDiff <= age()); diff --git a/encfs/MACFileIO.cpp b/encfs/MACFileIO.cpp index 4e2e350..bc03605 100644 --- a/encfs/MACFileIO.cpp +++ b/encfs/MACFileIO.cpp @@ -137,7 +137,9 @@ off_t MACFileIO::getSize() const { int bs = blockSize() + headerSize; off_t size = base->getSize(); - if (size > 0) size = locWithoutHeader(size, bs, headerSize); + if (size > 0) { + size = locWithoutHeader(size, bs, headerSize); + } return size; } @@ -202,7 +204,9 @@ ssize_t MACFileIO::readOneBlock(const IORequest &req) const { memcpy(req.data, tmp.data + headerSize, readSize); } else { VLOG(1) << "readSize " << readSize << " at offset " << req.offset; - if (readSize > 0) readSize = 0; + if (readSize > 0) { + readSize = 0; + } } MemoryPool::release(mb); @@ -256,7 +260,9 @@ int MACFileIO::truncate(off_t size) { int res = BlockFileIO::truncateBase(size, nullptr); - if (res == 0) base->truncate(locWithHeader(size, bs, headerSize)); + if (res == 0) { + base->truncate(locWithHeader(size, bs, headerSize)); + } return res; } diff --git a/encfs/MemoryPool.cpp b/encfs/MemoryPool.cpp index b76b212..ff28056 100644 --- a/encfs/MemoryPool.cpp +++ b/encfs/MemoryPool.cpp @@ -84,7 +84,9 @@ MemBlock MemoryPool::allocate(int size) { } pthread_mutex_unlock(&gMPoolMutex); - if (block == nullptr) block = allocBlock(size); + if (block == nullptr) { + block = allocBlock(size); + } block->next = nullptr; MemBlock result; diff --git a/encfs/NameIO.cpp b/encfs/NameIO.cpp index 98d2f2b..2d9582f 100644 --- a/encfs/NameIO.cpp +++ b/encfs/NameIO.cpp @@ -83,7 +83,9 @@ list NameIO::GetAlgorithmList(bool includeHidden) { bool NameIO::Register(const char *name, const char *description, const Interface &iface, Constructor constructor, bool hidden) { - if (gNameIOMap == nullptr) gNameIOMap = new NameIOMap_t; + if (gNameIOMap == nullptr) { + gNameIOMap = new NameIOMap_t; + } NameIOAlg alg; alg.hidden = hidden; @@ -163,7 +165,9 @@ std::string NameIO::recodePath( // figure out buffer sizes int approxLen = (this->*_length)(len); - if (approxLen <= 0) throw Error("Filename too small to decode"); + if (approxLen <= 0) { + throw Error("Filename too small to decode"); + } int bufSize = 0; BUFFER_INIT_S(codeBuf, 32, (unsigned int)approxLen + 1, bufSize) @@ -196,14 +200,18 @@ std::string NameIO::decodePath(const char *cipherPath) const { std::string NameIO::_encodePath(const char *plaintextPath, uint64_t *iv) const { // if chaining is not enabled, then the iv pointer is not used.. - if (!chainedNameIV) iv = nullptr; + if (!chainedNameIV) { + iv = nullptr; + } return recodePath(plaintextPath, &NameIO::maxEncodedNameLen, &NameIO::encodeName, iv); } std::string NameIO::_decodePath(const char *cipherPath, uint64_t *iv) const { // if chaining is not enabled, then the iv pointer is not used.. - if (!chainedNameIV) iv = nullptr; + if (!chainedNameIV) { + iv = nullptr; + } return recodePath(cipherPath, &NameIO::maxDecodedNameLen, &NameIO::decodeName, iv); } diff --git a/encfs/RawFileIO.cpp b/encfs/RawFileIO.cpp index 845ab8b..b8dae9a 100644 --- a/encfs/RawFileIO.cpp +++ b/encfs/RawFileIO.cpp @@ -69,9 +69,13 @@ RawFileIO::~RawFileIO() { swap(_fd, fd); swap(_oldfd, oldfd); - if (_oldfd != -1) close(_oldfd); + if (_oldfd != -1) { + close(_oldfd); + } - if (_fd != -1) close(_fd); + if (_fd != -1) { + close(_fd); + } } Interface RawFileIO::interface() const { return RawFileIO_iface; } @@ -126,7 +130,9 @@ int RawFileIO::open(int flags) { int finalFlags = requestWrite ? O_RDWR : O_RDONLY; #if defined(O_LARGEFILE) - if ((flags & O_LARGEFILE) != 0) finalFlags |= O_LARGEFILE; + if ((flags & O_LARGEFILE) != 0) { + finalFlags |= O_LARGEFILE; + } #else #warning O_LARGEFILE not supported #endif @@ -238,7 +244,9 @@ bool RawFileIO::write(const IORequest &req) { } if (knownSize) { off_t last = req.offset + req.dataLen; - if (last > fileSize) fileSize = last; + if (last > fileSize) { + fileSize = last; + } } return true; diff --git a/encfs/SSL_Cipher.cpp b/encfs/SSL_Cipher.cpp index b23da1a..f2b86a8 100644 --- a/encfs/SSL_Cipher.cpp +++ b/encfs/SSL_Cipher.cpp @@ -81,7 +81,9 @@ int BytesToKey(int keyLen, int ivLen, const EVP_MD *md, for (;;) { EVP_DigestInit_ex(cx, md, nullptr); - if ((addmd++) != 0) EVP_DigestUpdate(cx, mdBuf, mds); + if ((addmd++) != 0) { + EVP_DigestUpdate(cx, mdBuf, mds); + } EVP_DigestUpdate(cx, data, dataLen); EVP_DigestFinal_ex(cx, mdBuf, &mds); @@ -106,7 +108,9 @@ int BytesToKey(int keyLen, int ivLen, const EVP_MD *md, niv -= toCopy; offset += toCopy; } - if ((nkey == 0) && (niv == 0)) break; + if ((nkey == 0) && (niv == 0)) { + break; + } } EVP_MD_CTX_free(cx); OPENSSL_cleanse(mdBuf, sizeof(mdBuf)); @@ -130,7 +134,9 @@ int TimedPBKDF2(const char *pass, int passlen, const unsigned char *salt, int res = PKCS5_PBKDF2_HMAC_SHA1(pass, passlen, const_cast(salt), saltlen, iter, keylen, out); - if (res != 1) return -1; + if (res != 1) { + return -1; + } gettimeofday(&end, nullptr); @@ -162,7 +168,9 @@ static Range BFKeyRange(128, 256, 32); static Range BFBlockRange(64, 4096, 8); static std::shared_ptr NewBFCipher(const Interface &iface, int keyLen) { - if (keyLen <= 0) keyLen = 160; + if (keyLen <= 0) { + keyLen = 160; + } keyLen = BFKeyRange.closest(keyLen); @@ -187,7 +195,9 @@ static Range AESBlockRange(64, 4096, 16); static std::shared_ptr NewAESCipher(const Interface &iface, int keyLen) { - if (keyLen <= 0) keyLen = 192; + if (keyLen <= 0) { + keyLen = 192; + } keyLen = AESKeyRange.closest(keyLen); @@ -485,7 +495,9 @@ static uint64_t _checksum_64(SSLKey *key, const unsigned char *data, } auto value = (uint64_t)h[0]; - for (int i = 1; i < 8; ++i) value = (value << 8) | (uint64_t)h[i]; + for (int i = 1; i < 8; ++i) { + value = (value << 8) | (uint64_t)h[i]; + } return value; } @@ -519,7 +531,9 @@ uint64_t SSL_Cipher::MAC_64(const unsigned char *data, int len, std::shared_ptr mk = dynamic_pointer_cast(key); uint64_t tmp = _checksum_64(mk.get(), data, len, chainedIV); - if (chainedIV != nullptr) *chainedIV = tmp; + if (chainedIV != nullptr) { + *chainedIV = tmp; + } return tmp; } @@ -702,7 +716,9 @@ static void flipBytes(unsigned char *buf, int size) { while (bytesLeft != 0) { int toFlip = MIN(sizeof(revBuf), bytesLeft); - for (int i = 0; i < toFlip; ++i) revBuf[i] = buf[toFlip - (i + 1)]; + for (int i = 0; i < toFlip; ++i) { + revBuf[i] = buf[toFlip - (i + 1)]; + } memcpy(buf, revBuf, toFlip); bytesLeft -= toFlip; @@ -712,11 +728,15 @@ static void flipBytes(unsigned char *buf, int size) { } static void shuffleBytes(unsigned char *buf, int size) { - for (int i = 0; i < size - 1; ++i) buf[i + 1] ^= buf[i]; + for (int i = 0; i < size - 1; ++i) { + buf[i + 1] ^= buf[i]; + } } static void unshuffleBytes(unsigned char *buf, int size) { - for (int i = size - 1; i != 0; --i) buf[i] ^= buf[i - 1]; + for (int i = size - 1; i != 0; --i) { + buf[i] ^= buf[i - 1]; + } } /** Partial blocks are encoded with a stream cipher. We make multiple passes on diff --git a/encfs/StreamNameIO.cpp b/encfs/StreamNameIO.cpp index a2b1649..682d32b 100644 --- a/encfs/StreamNameIO.cpp +++ b/encfs/StreamNameIO.cpp @@ -96,7 +96,9 @@ int StreamNameIO::encodeName(const char *plaintextName, int length, uint64_t *iv, char *encodedName, int bufferLength) const { uint64_t tmpIV = 0; - if ((iv != nullptr) && _interface >= 2) tmpIV = *iv; + if ((iv != nullptr) && _interface >= 2) { + tmpIV = *iv; + } unsigned int mac = _cipher->MAC_16((const unsigned char *)plaintextName, length, _key, iv); @@ -137,7 +139,9 @@ int StreamNameIO::decodeName(const char *encodedName, int length, uint64_t *iv, int decodedStreamLen = decLen256 - 2; rAssert(decodedStreamLen <= bufferLength); - if (decodedStreamLen <= 0) throw Error("Filename too small to decode"); + if (decodedStreamLen <= 0) { + throw Error("Filename too small to decode"); + } BUFFER_INIT(tmpBuf, 32, (unsigned int)length); @@ -155,7 +159,9 @@ int StreamNameIO::decodeName(const char *encodedName, int length, uint64_t *iv, ((unsigned int)((unsigned char)tmpBuf[1])); // version 2 adds support for IV chaining.. - if ((iv != nullptr) && _interface >= 2) tmpIV = *iv; + if ((iv != nullptr) && _interface >= 2) { + tmpIV = *iv; + } memcpy(plaintextName, tmpBuf + 2, decodedStreamLen); } else { diff --git a/encfs/XmlReader.cpp b/encfs/XmlReader.cpp index e914e2f..1e4cd20 100644 --- a/encfs/XmlReader.cpp +++ b/encfs/XmlReader.cpp @@ -46,7 +46,9 @@ XmlValuePtr XmlValue::find(const char *path) const { bool XmlValue::read(const char *path, std::string *out) const { XmlValuePtr value = find(path); - if (!value) return false; + if (!value) { + return false; + } *out = value->text(); return true; @@ -54,7 +56,9 @@ bool XmlValue::read(const char *path, std::string *out) const { bool XmlValue::read(const char *path, int *out) const { XmlValuePtr value = find(path); - if (!value) return false; + if (!value) { + return false; + } *out = atoi(value->text().c_str()); return true; @@ -62,7 +66,9 @@ bool XmlValue::read(const char *path, int *out) const { bool XmlValue::read(const char *path, long *out) const { XmlValuePtr value = find(path); - if (!value) return false; + if (!value) { + return false; + } *out = atol(value->text().c_str()); return true; @@ -70,7 +76,9 @@ bool XmlValue::read(const char *path, long *out) const { bool XmlValue::read(const char *path, double *out) const { XmlValuePtr value = find(path); - if (!value) return false; + if (!value) { + return false; + } *out = atof(value->text().c_str()); return true; @@ -78,7 +86,9 @@ bool XmlValue::read(const char *path, double *out) const { bool XmlValue::read(const char *path, bool *out) const { XmlValuePtr value = find(path); - if (!value) return false; + if (!value) { + return false; + } *out = (atoi(value->text().c_str()) != 0); return true; @@ -87,7 +97,9 @@ bool XmlValue::read(const char *path, bool *out) const { bool XmlValue::readB64(const char *path, unsigned char *data, int length) const { XmlValuePtr value = find(path); - if (!value) return false; + if (!value) { + return false; + } std::string s = value->text(); s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end()); @@ -110,7 +122,9 @@ bool XmlValue::readB64(const char *path, unsigned char *data, bool XmlValue::read(const char *path, Interface *out) const { XmlValuePtr node = find(path); - if (!node) return false; + if (!node) { + return false; + } bool ok = node->read("name", &out->name()) && node->read("major", &out->current()) && @@ -121,12 +135,16 @@ bool XmlValue::read(const char *path, Interface *out) const { std::string safeValueForNode(const tinyxml2::XMLElement *element) { std::string value; - if (element == nullptr) return value; + if (element == nullptr) { + return value; + } const tinyxml2::XMLNode *child = element->FirstChild(); if (child != nullptr) { const tinyxml2::XMLText *childText = child->ToText(); - if (childText != nullptr) value = childText->Value(); + if (childText != nullptr) { + value = childText->Value(); + } } return value; @@ -169,7 +187,9 @@ bool XmlReader::load(const char *fileName) { pd->doc.reset(new tinyxml2::XMLDocument()); std::ifstream in(fileName); - if (!in) return false; + if (!in) { + return false; + } std::ostringstream fileContent; fileContent << in.rdbuf(); diff --git a/encfs/autosprintf.cpp b/encfs/autosprintf.cpp index 33bbe32..26a27a8 100644 --- a/encfs/autosprintf.cpp +++ b/encfs/autosprintf.cpp @@ -38,7 +38,9 @@ namespace gnu { autosprintf::autosprintf(const char *format, ...) { va_list args; va_start(args, format); - if (vasprintf(&str, format, args) < 0) str = nullptr; + if (vasprintf(&str, format, args) < 0) { + str = nullptr; + } va_end(args); } diff --git a/encfs/base64.cpp b/encfs/base64.cpp index 38de109..37b6d6c 100644 --- a/encfs/base64.cpp +++ b/encfs/base64.cpp @@ -52,7 +52,9 @@ void changeBase2(unsigned char *src, int srcLen, int src2Pow, } // now, we could have a partial value left in the work buffer.. - if ((workBits != 0) && ((dst - origDst) < dstLen)) *dst++ = work & mask; + if ((workBits != 0) && ((dst - origDst) < dstLen)) { + *dst++ = work & mask; + } } /* @@ -67,7 +69,9 @@ static void changeBase2Inline(unsigned char *src, int srcLen, int src2Pow, unsigned long work, int workBits, unsigned char *outLoc) { const int mask = (1 << dst2Pow) - 1; - if (outLoc == nullptr) outLoc = src; + if (outLoc == nullptr) { + outLoc = src; + } // copy the new bits onto the high bits of the stream. // The bits that fall off the low end are the output bits. diff --git a/encfs/encfs.cpp b/encfs/encfs.cpp index 816125d..8787719 100644 --- a/encfs/encfs.cpp +++ b/encfs/encfs.cpp @@ -76,7 +76,9 @@ static EncFS_Context *context() { * if the argument is NULL. */ static bool isReadOnly(EncFS_Context *ctx) { - if (ctx == nullptr) ctx = (EncFS_Context *)fuse_get_context()->private_data; + if (ctx == nullptr) { + ctx = (EncFS_Context *)fuse_get_context()->private_data; + } return ctx->opts->readOnly; } @@ -89,7 +91,9 @@ static int withCipherPath(const char *opName, const char *path, int res = -EIO; std::shared_ptr FSRoot = ctx->getRoot(&res); - if (!FSRoot) return res; + if (!FSRoot) { + return res; + } try { string cyName = FSRoot->cipherPath(path); @@ -140,7 +144,9 @@ static int withFileNode(const char *opName, const char *path, int res = -EIO; std::shared_ptr FSRoot = ctx->getRoot(&res); - if (!FSRoot) return res; + if (!FSRoot) { + return res; + } try { @@ -230,7 +236,9 @@ int encfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, int res = ESUCCESS; std::shared_ptr FSRoot = ctx->getRoot(&res); - if (!FSRoot) return res; + if (!FSRoot) { + return res; + } try { @@ -252,7 +260,9 @@ int encfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, #if defined(fuse_fill_dir_flags) if (filler(buf, name.c_str(), &st, 0, 0)) break; #else - if (filler(buf, name.c_str(), &st, 0) != 0) break; + if (filler(buf, name.c_str(), &st, 0) != 0) { + break; + } #endif name = dt.nextPlaintextName(&fileType, &inode); @@ -271,11 +281,15 @@ int encfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, int encfs_mknod(const char *path, mode_t mode, dev_t rdev) { EncFS_Context *ctx = context(); - if (isReadOnly(ctx)) return -EROFS; + if (isReadOnly(ctx)) { + return -EROFS; + } int res = -EIO; std::shared_ptr FSRoot = ctx->getRoot(&res); - if (!FSRoot) return res; + if (!FSRoot) { + return res; + } try { std::shared_ptr fnode = FSRoot->lookupNode(path, "mknod"); @@ -314,11 +328,15 @@ int encfs_mkdir(const char *path, mode_t mode) { fuse_context *fctx = fuse_get_context(); EncFS_Context *ctx = context(); - if (isReadOnly(ctx)) return -EROFS; + if (isReadOnly(ctx)) { + return -EROFS; + } int res = -EIO; std::shared_ptr FSRoot = ctx->getRoot(&res); - if (!FSRoot) return res; + if (!FSRoot) { + return res; + } try { uid_t uid = 0; @@ -349,11 +367,15 @@ int encfs_mkdir(const char *path, mode_t mode) { int encfs_unlink(const char *path) { EncFS_Context *ctx = context(); - if (isReadOnly(ctx)) return -EROFS; + if (isReadOnly(ctx)) { + return -EROFS; + } int res = -EIO; std::shared_ptr FSRoot = ctx->getRoot(&res); - if (!FSRoot) return res; + if (!FSRoot) { + return res; + } try { // let DirNode handle it atomically so that it can handle race @@ -370,7 +392,9 @@ int _do_rmdir(EncFS_Context *, const string &cipherPath) { } int encfs_rmdir(const char *path) { - if (isReadOnly(nullptr)) return -EROFS; + if (isReadOnly(nullptr)) { + return -EROFS; + } return withCipherPath("rmdir", path, bind(_do_rmdir, _1, _2)); } @@ -378,11 +402,15 @@ int _do_readlink(EncFS_Context *ctx, const string &cyName, char *buf, size_t size) { int res = ESUCCESS; std::shared_ptr FSRoot = ctx->getRoot(&res); - if (!FSRoot) return res; + if (!FSRoot) { + return res; + } res = ::readlink(cyName.c_str(), buf, size - 1); - if (res == -1) return -errno; + if (res == -1) { + return -errno; + } buf[res] = '\0'; // ensure null termination string decodedName; @@ -413,11 +441,15 @@ int encfs_readlink(const char *path, char *buf, size_t size) { int encfs_symlink(const char *to, const char *from) { EncFS_Context *ctx = context(); - if (isReadOnly(ctx)) return -EROFS; + if (isReadOnly(ctx)) { + return -EROFS; + } int res = -EIO; std::shared_ptr FSRoot = ctx->getRoot(&res); - if (!FSRoot) return res; + if (!FSRoot) { + return res; + } try { string fromCName = FSRoot->cipherPath(from); @@ -436,8 +468,12 @@ int encfs_symlink(const char *to, const char *from) { oldgid = setfsgid(context->gid); } res = ::symlink(toCName.c_str(), fromCName.c_str()); - if (olduid >= 0) setfsuid(olduid); - if (oldgid >= 0) setfsgid(oldgid); + if (olduid >= 0) { + setfsuid(olduid); + } + if (oldgid >= 0) { + setfsgid(oldgid); + } if (res == -1) { res = -errno; @@ -453,11 +489,15 @@ int encfs_symlink(const char *to, const char *from) { int encfs_link(const char *from, const char *to) { EncFS_Context *ctx = context(); - if (isReadOnly(ctx)) return -EROFS; + if (isReadOnly(ctx)) { + return -EROFS; + } int res = -EIO; std::shared_ptr FSRoot = ctx->getRoot(&res); - if (!FSRoot) return res; + if (!FSRoot) { + return res; + } try { res = FSRoot->link(from, to); @@ -470,11 +510,15 @@ int encfs_link(const char *from, const char *to) { int encfs_rename(const char *from, const char *to) { EncFS_Context *ctx = context(); - if (isReadOnly(ctx)) return -EROFS; + if (isReadOnly(ctx)) { + return -EROFS; + } int res = -EIO; std::shared_ptr FSRoot = ctx->getRoot(&res); - if (!FSRoot) return res; + if (!FSRoot) { + return res; + } try { res = FSRoot->rename(from, to); @@ -489,7 +533,9 @@ int _do_chmod(EncFS_Context *, const string &cipherPath, mode_t mode) { } int encfs_chmod(const char *path, mode_t mode) { - if (isReadOnly(nullptr)) return -EROFS; + if (isReadOnly(nullptr)) { + return -EROFS; + } return withCipherPath("chmod", path, bind(_do_chmod, _1, _2, mode)); } @@ -499,19 +545,25 @@ int _do_chown(EncFS_Context *, const string &cyName, uid_t u, gid_t g) { } int encfs_chown(const char *path, uid_t uid, gid_t gid) { - if (isReadOnly(nullptr)) return -EROFS; + if (isReadOnly(nullptr)) { + return -EROFS; + } return withCipherPath("chown", path, bind(_do_chown, _1, _2, uid, gid)); } int _do_truncate(FileNode *fnode, off_t size) { return fnode->truncate(size); } int encfs_truncate(const char *path, off_t size) { - if (isReadOnly(nullptr)) return -EROFS; + if (isReadOnly(nullptr)) { + return -EROFS; + } return withFileNode("truncate", path, nullptr, bind(_do_truncate, _1, size)); } int encfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) { - if (isReadOnly(nullptr)) return -EROFS; + if (isReadOnly(nullptr)) { + return -EROFS; + } return withFileNode("ftruncate", path, fi, bind(_do_truncate, _1, size)); } @@ -521,7 +573,9 @@ int _do_utime(EncFS_Context *, const string &cyName, struct utimbuf *buf) { } int encfs_utime(const char *path, struct utimbuf *buf) { - if (isReadOnly(nullptr)) return -EROFS; + if (isReadOnly(nullptr)) { + return -EROFS; + } return withCipherPath("utime", path, bind(_do_utime, _1, _2, buf)); } @@ -542,7 +596,9 @@ int _do_utimens(EncFS_Context *, const string &cyName, } int encfs_utimens(const char *path, const struct timespec ts[2]) { - if (isReadOnly(nullptr)) return -EROFS; + if (isReadOnly(nullptr)) { + return -EROFS; + } return withCipherPath("utimens", path, bind(_do_utimens, _1, _2, ts)); } @@ -556,7 +612,9 @@ int encfs_open(const char *path, struct fuse_file_info *file) { int res = -EIO; std::shared_ptr FSRoot = ctx->getRoot(&res); - if (!FSRoot) return res; + if (!FSRoot) { + return res; + } try { std::shared_ptr fnode = @@ -647,7 +705,9 @@ int _do_fsync(FileNode *fnode, int dataSync) { } int encfs_fsync(const char *path, int dataSync, struct fuse_file_info *file) { - if (isReadOnly(nullptr)) return -EROFS; + if (isReadOnly(nullptr)) { + return -EROFS; + } return withFileNode("fsync", path, file, bind(_do_fsync, _1, dataSync)); } @@ -660,7 +720,9 @@ int _do_write(FileNode *fnode, unsigned char *ptr, size_t size, off_t offset) { int encfs_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *file) { - if (isReadOnly(nullptr)) return -EROFS; + if (isReadOnly(nullptr)) { + return -EROFS; + } return withFileNode("write", path, file, bind(_do_write, _1, (unsigned char *)buf, size, offset)); } @@ -681,7 +743,9 @@ int encfs_statfs(const char *path, struct statvfs *st) { // adjust maximum name length.. st->f_namemax = 6 * (st->f_namemax - 2) / 8; // approx.. } - if (res == -1) res = -errno; + if (res == -1) { + res = -errno; + } } catch (encfs::Error &err) { RLOG(ERROR) << "error caught in statfs: " << err.what(); } @@ -710,7 +774,9 @@ int _do_setxattr(EncFS_Context *, const string &cyName, const char *name, } int encfs_setxattr(const char *path, const char *name, const char *value, size_t size, int flags) { - if (isReadOnly(nullptr)) return -EROFS; + if (isReadOnly(nullptr)) { + return -EROFS; + } return withCipherPath("setxattr", path, bind(_do_setxattr, _1, _2, name, value, size, flags)); } @@ -768,7 +834,9 @@ int _do_removexattr(EncFS_Context *, const string &cyName, const char *name) { } int encfs_removexattr(const char *path, const char *name) { - if (isReadOnly(nullptr)) return -EROFS; + if (isReadOnly(nullptr)) { + return -EROFS; + } return withCipherPath("removexattr", path, bind(_do_removexattr, _1, _2, name)); diff --git a/encfs/main.cpp b/encfs/main.cpp index e22168e..0d0e6bb 100644 --- a/encfs/main.cpp +++ b/encfs/main.cpp @@ -85,15 +85,33 @@ struct EncFS_Args { ostringstream ss; ss << (isDaemon ? "(daemon) " : "(fg) "); ss << (isThreaded ? "(threaded) " : "(UP) "); - if (idleTimeout > 0) ss << "(timeout " << idleTimeout << ") "; - if (opts->checkKey) ss << "(keyCheck) "; - if (opts->forceDecode) ss << "(forceDecode) "; - if (opts->ownerCreate) ss << "(ownerCreate) "; - if (opts->useStdin) ss << "(useStdin) "; - if (opts->annotate) ss << "(annotate) "; - if (opts->reverseEncryption) ss << "(reverseEncryption) "; - if (opts->mountOnDemand) ss << "(mountOnDemand) "; - if (opts->delayMount) ss << "(delayMount) "; + if (idleTimeout > 0) { + ss << "(timeout " << idleTimeout << ") "; + } + if (opts->checkKey) { + ss << "(keyCheck) "; + } + if (opts->forceDecode) { + ss << "(forceDecode) "; + } + if (opts->ownerCreate) { + ss << "(ownerCreate) "; + } + if (opts->useStdin) { + ss << "(useStdin) "; + } + if (opts->annotate) { + ss << "(annotate) "; + } + if (opts->reverseEncryption) { + ss << "(reverseEncryption) "; + } + if (opts->mountOnDemand) { + ss << "(mountOnDemand) "; + } + if (opts->delayMount) { + ss << "(delayMount) "; + } for (int i = 0; i < fuseArgc; ++i) { ss << fuseArgv[i] << ' '; } @@ -174,7 +192,9 @@ static void FuseUsage() { static string slashTerminate(const string &src) { string result = src; - if (result[result.length() - 1] != '/') result.append("/"); + if (result[result.length() - 1] != '/') { + result.append("/"); + } return result; } @@ -249,7 +269,9 @@ static bool processArgs(int argc, char *argv[], int res = getopt_long(argc, argv, "HsSfvdmi:o:t:", long_options, &option_index); - if (res == -1) break; + if (res == -1) { + break; + } switch (res) { case '1': @@ -381,7 +403,9 @@ static bool processArgs(int argc, char *argv[], } } - if (!out->isThreaded) PUSHARG("-s"); + if (!out->isThreaded) { + PUSHARG("-s"); + } // we should have at least 2 arguments left over - the source directory and // the mount point. @@ -640,7 +664,9 @@ int main(int argc, char *argv[]) { try { time_t startTime, endTime; - if (encfsArgs->opts->annotate) cerr << "$STATUS$ fuse_main_start" << endl; + if (encfsArgs->opts->annotate) { + cerr << "$STATUS$ fuse_main_start" << endl; + } // FIXME: workaround for fuse_main returning an error on normal // exit. Only print information if fuse_main returned @@ -654,9 +680,13 @@ int main(int argc, char *argv[]) { time(&endTime); - if (encfsArgs->opts->annotate) cerr << "$STATUS$ fuse_main_end" << endl; + if (encfsArgs->opts->annotate) { + cerr << "$STATUS$ fuse_main_end" << endl; + } - if (res == 0) returnCode = EXIT_SUCCESS; + if (res == 0) { + returnCode = EXIT_SUCCESS; + } if (res != 0 && encfsArgs->isDaemon && (oldStderr >= 0) && (endTime - startTime <= 1)) { diff --git a/encfs/openssl.cpp b/encfs/openssl.cpp index c1761ba..3b0f1b3 100644 --- a/encfs/openssl.cpp +++ b/encfs/openssl.cpp @@ -97,7 +97,9 @@ void openssl_shutdown(bool threaded) { ENGINE_cleanup(); #endif - if (threaded) pthreads_locking_cleanup(); + if (threaded) { + pthreads_locking_cleanup(); + } } } // namespace encfs diff --git a/encfs/readpassphrase.cpp b/encfs/readpassphrase.cpp index ebbafe1..59a8abd 100644 --- a/encfs/readpassphrase.cpp +++ b/encfs/readpassphrase.cpp @@ -112,7 +112,9 @@ restart: /* Turn off echo if possible. */ if (tcgetattr(input, &oterm) == 0) { memcpy(&term, &oterm, sizeof(term)); - if ((flags & RPP_ECHO_ON) == 0) term.c_lflag &= ~(ECHO | ECHONL); + if ((flags & RPP_ECHO_ON) == 0) { + term.c_lflag &= ~(ECHO | ECHONL); + } #ifdef VSTATUS if (term.c_cc[VSTATUS] != _POSIX_VDISABLE) term.c_cc[VSTATUS] = _POSIX_VDISABLE; @@ -127,17 +129,25 @@ restart: end = buf + bufsiz - 1; for (p = buf; (nr = read(input, &ch, 1)) == 1 && ch != '\n' && ch != '\r';) { if (p < end) { - if ((flags & RPP_SEVENBIT) != 0) ch &= 0x7f; + if ((flags & RPP_SEVENBIT) != 0) { + ch &= 0x7f; + } if (isalpha(ch) != 0) { - if ((flags & RPP_FORCELOWER) != 0) ch = tolower(ch); - if ((flags & RPP_FORCEUPPER) != 0) ch = toupper(ch); + if ((flags & RPP_FORCELOWER) != 0) { + ch = tolower(ch); + } + if ((flags & RPP_FORCEUPPER) != 0) { + ch = toupper(ch); + } } *p++ = ch; } } *p = '\0'; save_errno = errno; - if ((term.c_lflag & ECHO) == 0u) (void)write(output, "\n", 1); + if ((term.c_lflag & ECHO) == 0u) { + (void)write(output, "\n", 1); + } /* Restore old terminal settings and signals. */ if (memcmp(&term, &oterm, sizeof(term)) != 0) { @@ -150,7 +160,9 @@ restart: (void)sigaction(SIGTSTP, &savetstp, nullptr); (void)sigaction(SIGTTIN, &savettin, nullptr); (void)sigaction(SIGTTOU, &savettou, nullptr); - if (input != STDIN_FILENO) (void)close(input); + if (input != STDIN_FILENO) { + (void)close(input); + } /* * If we were interrupted by a signal, resend it to ourselves From c947c684c7fc78508565dbfb7376849c6b07a938 Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Thu, 3 Aug 2017 23:26:12 -0700 Subject: [PATCH 09/10] lint: misc cleanup, including possible NPE --- encfs/Context.cpp | 2 +- encfs/DirNode.cpp | 26 +++++++++++++------------- encfs/FileUtils.cpp | 17 +++++------------ encfs/SSL_Cipher.cpp | 10 +++------- 4 files changed, 22 insertions(+), 33 deletions(-) diff --git a/encfs/Context.cpp b/encfs/Context.cpp index 16ec724..f8151b9 100644 --- a/encfs/Context.cpp +++ b/encfs/Context.cpp @@ -75,7 +75,7 @@ void EncFS_Context::setRoot(const std::shared_ptr &r) { } } -bool EncFS_Context::isMounted() { return root.get() != nullptr; } +bool EncFS_Context::isMounted() { return root != nullptr; } void EncFS_Context::getAndResetUsageCounter(int *usage, int *openCount) { Lock lock(contextMutex); diff --git a/encfs/DirNode.cpp b/encfs/DirNode.cpp index 1c0f78c..2494084 100644 --- a/encfs/DirNode.cpp +++ b/encfs/DirNode.cpp @@ -149,7 +149,7 @@ class RenameOp { ~RenameOp(); - operator bool() const { return renameList.get() != nullptr; } + operator bool() const { return renameList != nullptr; } bool apply(); void undo(); @@ -659,21 +659,21 @@ std::shared_ptr DirNode::findOrCreate(const char *plainName) { // See if we already have a FileNode for this path. if (ctx != nullptr) { node = ctx->lookupNode(plainName); - } - // If we don't, create a new one. - if (!node) { - uint64_t iv = 0; - string cipherName = naming->encodePath(plainName, &iv); - uint64_t fuseFh = ctx->nextFuseFh(); - node.reset(new FileNode(this, fsConfig, plainName, - (rootDir + cipherName).c_str(), fuseFh)); + // If we don't, create a new one. + if (!node) { + uint64_t iv = 0; + string cipherName = naming->encodePath(plainName, &iv); + uint64_t fuseFh = ctx->nextFuseFh(); + node.reset(new FileNode(this, fsConfig, plainName, + (rootDir + cipherName).c_str(), fuseFh)); - if (fsConfig->config->externalIVChaining) { - node->setName(nullptr, nullptr, iv); + if (fsConfig->config->externalIVChaining) { + node->setName(nullptr, nullptr, iv); + } + + VLOG(1) << "created FileNode for " << node->cipherName(); } - - VLOG(1) << "created FileNode for " << node->cipherName(); } return node; diff --git a/encfs/FileUtils.cpp b/encfs/FileUtils.cpp index 4e67ac9..3374ce5 100644 --- a/encfs/FileUtils.cpp +++ b/encfs/FileUtils.cpp @@ -125,11 +125,7 @@ EncFS_Root::~EncFS_Root() = default; bool fileExists(const char *fileName) { struct stat buf; - if (lstat(fileName, &buf) == 0) { - return true; - } - // XXX show perror? - return false; + return lstat(fileName, &buf) == 0; } bool isDirectory(const char *fileName) { @@ -141,10 +137,7 @@ bool isDirectory(const char *fileName) { } bool isAbsolutePath(const char *fileName) { - if ((fileName != nullptr) && fileName[0] != '\0' && fileName[0] == '/') { - return true; - } - return false; + return (fileName != nullptr) && fileName[0] != '\0' && fileName[0] == '/'; } const char *lastPathElement(const char *name) { @@ -1306,7 +1299,7 @@ void showFSInfo(const EncFSConfig *config) { cout << "\n"; } } - if (config->kdfIterations > 0 && config->salt.size() > 0) { + if (config->kdfIterations > 0 && !config->salt.empty()) { cout << autosprintf(_("Using PBKDF2, with %i iterations"), config->kdfIterations) << "\n"; @@ -1389,12 +1382,12 @@ CipherKey EncFSConfig::makeKey(const char *password, int passwdLen) { // if no salt is set and we're creating a new password for a new // FS type, then initialize salt.. - if (salt.size() == 0 && kdfIterations == 0 && cfgType >= Config_V6) { + if (salt.empty() && kdfIterations == 0 && cfgType >= Config_V6) { // upgrade to using salt salt.resize(20); } - if (salt.size() > 0) { + if (!salt.empty()) { // if iterations isn't known, then we're creating a new key, so // randomize the salt.. if (kdfIterations == 0 && diff --git a/encfs/SSL_Cipher.cpp b/encfs/SSL_Cipher.cpp index f2b86a8..d0cb1fd 100644 --- a/encfs/SSL_Cipher.cpp +++ b/encfs/SSL_Cipher.cpp @@ -414,9 +414,8 @@ CipherKey SSL_Cipher::newKey(const char *password, int passwdLength) { } } else { // for backward compatibility with filesystems created with 1:0 - bytes = EVP_BytesToKey(_blockCipher, EVP_sha1(), nullptr, - (unsigned char *)password, passwdLength, 16, - KeyData(key), IVData(key)); + EVP_BytesToKey(_blockCipher, EVP_sha1(), nullptr, (unsigned char *)password, + passwdLength, 16, KeyData(key), IVData(key)); } initKey(key, _blockCipher, _streamCipher, _keySize); @@ -610,10 +609,7 @@ bool SSL_Cipher::compareKey(const CipherKey &A, const CipherKey &B) const { rAssert(key1->keySize == _keySize); rAssert(key2->keySize == _keySize); - if (memcmp(key1->buffer, key2->buffer, _keySize + _ivLength) != 0) { - return false; - } - return true; + return memcmp(key1->buffer, key2->buffer, _keySize + _ivLength) == 0; } int SSL_Cipher::encodedKeySize() const { From f953a87c6614dd02e4653476ebbff085383fd7ac Mon Sep 17 00:00:00 2001 From: Valient Gough Date: Thu, 3 Aug 2017 23:26:32 -0700 Subject: [PATCH 10/10] disable some lint checks --- CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c730c5..4829e25 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -146,7 +146,8 @@ if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.5) # Need 3.6 or abo message(STATUS "clang-tidy not found.") else() message(STATUS "clang-tidy found: ${CLANG_TIDY_EXE}") - set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}" "-checks=*,-clang-analyzer-alpha.*") + set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}" "-checks=*,-modernize-loop-convert,-cppcoreguidelines-pro-*,-readability-inconsistent-declaration-parameter-name,-google-readability-casting,-cert-err58-cpp,-google-runtime-int,-readability-named-parameter,-google-build-using-namespace,-misc-unused-parameters,-google-runtime-references") + #set(DO_CLANG_TIDY "${CLANG_TIDY_EXE}" "-fix" "-checks=-*,google-readability-redundant-smartptr-get") endif() endif()