Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
avoid some useless casts reported by -Wuseless-cast
Browse files Browse the repository at this point in the history
The casts are useless, but the reports show some where we can actually
improve the code by replacing them with better alternatives like
converting whatever int type into a string instead of casting to a
specific one which might in the future be too small.

Reported-By: gcc -Wuseless-cast
  • Loading branch information
DonKult committed Dec 13, 2017
1 parent 957381a commit 1adcf56
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 43 deletions.
2 changes: 1 addition & 1 deletion apt-pkg/cdrom.cc
Expand Up @@ -315,7 +315,7 @@ bool pkgCdrom::DropRepeats(vector<string> &List,const char *Name)
for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
c != compressor.end(); ++c)
{
std::string filename = std::string(List[I]).append(Name).append(c->Extension);
std::string const filename = List[I] + Name + c->Extension;
if (stat(filename.c_str(), &Buf) != 0)
continue;
Inodes[I] = Buf.st_ino;
Expand Down
33 changes: 14 additions & 19 deletions apt-pkg/contrib/cdromutl.cc
Expand Up @@ -105,13 +105,14 @@ bool UnmountCdrom(string Path)
}
else
{
const char *Args[10];
Args[0] = "umount";
Args[1] = Path.c_str();
Args[2] = 0;
execvp(Args[0],(char **)Args);
const char * const Args[] = {
"umount",
Path.c_str(),
nullptr
};
execvp(Args[0], const_cast<char **>(Args));
_exit(100);
}
}
}

// if it can not be umounted, give it a bit more time
Expand Down Expand Up @@ -222,15 +223,13 @@ bool IdentCdrom(string CD,string &Res,unsigned int Version)

std::string S;
if (Version <= 1)
{
strprintf(S, "%lu", (unsigned long)Dir->d_ino);
}
S = std::to_string(Dir->d_ino);
else
{
struct stat Buf;
if (fstatat(dirfd, Dir->d_name, &Buf, 0) != 0)
continue;
strprintf(S, "%lu", (unsigned long)Buf.st_mtime);
S = std::to_string(Buf.st_mtime);
}

Hash.Add(S.c_str());
Expand All @@ -246,21 +245,17 @@ bool IdentCdrom(string CD,string &Res,unsigned int Version)
return _error->Errno("statfs",_("Failed to stat the cdrom"));

// We use a kilobyte block size to avoid overflow
if (writable_media)
{
strprintf(S, "%lu", (unsigned long)(Buf.f_blocks*(Buf.f_bsize/1024)));
} else {
strprintf(S, "%lu %lu", (unsigned long)(Buf.f_blocks*(Buf.f_bsize/1024)),
(unsigned long)(Buf.f_bfree*(Buf.f_bsize/1024)));
}
Hash.Add(S.c_str());
S = std::to_string(Buf.f_blocks * (Buf.f_bsize / 1024));
if (writable_media == false)
S.append(" ").append(std::to_string(Buf.f_bfree * (Buf.f_bsize / 1024)));
Hash.Add(S.c_str(), S.length());
strprintf(S, "-%u", Version);
}
else
strprintf(S, "-%u.debug", Version);

closedir(D);
Res = Hash.Result().Value() + S;
Res = Hash.Result().Value().append(std::move(S));
return true;
}
/*}}}*/
Expand Down
15 changes: 7 additions & 8 deletions apt-pkg/indexcopy.cc
Expand Up @@ -58,15 +58,14 @@ bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List,
// Prepare the progress indicator
off_t TotalSize = 0;
std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors();
for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
for (auto const &F : List)
{
struct stat Buf;
bool found = false;
std::string file = std::string(*I).append(GetFileName());
for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
c != compressor.end(); ++c)
auto const file = F + GetFileName();
for (auto const &ext: APT::Configuration::getCompressorExtensions())
{
if (stat((file + c->Extension).c_str(), &Buf) != 0)
if (stat((file + ext).c_str(), &Buf) != 0)
continue;
found = true;
break;
Expand All @@ -82,9 +81,9 @@ bool IndexCopy::CopyPackages(string CDROM,string Name,vector<string> &List,
unsigned int WrongSize = 0;
unsigned int Packages = 0;
for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
{
string OrigPath = string(*I,CDROM.length());
{
std::string OrigPath(*I,CDROM.length());

// Open the package file
FileFd Pkg(*I + GetFileName(), FileFd::ReadOnly, FileFd::Auto);
off_t const FileSize = Pkg.Size();
Expand Down
7 changes: 1 addition & 6 deletions apt-private/private-source.cc
Expand Up @@ -208,12 +208,7 @@ static pkgSrcRecords::Parser *FindSrc(const char *Name,
// or RelTag
if (Cache.BuildPolicy() == false)
return nullptr;
pkgPolicy * Policy = dynamic_cast<pkgPolicy*>(Cache.GetPolicy());
if (Policy == nullptr)
{
_error->Fatal("Implementation error: dynamic up-casting policy engine failed in FindSrc!");
return nullptr;
}
pkgPolicy * const Policy = Cache.GetPolicy();
pkgCache::VerIterator const Ver = Policy->GetCandidateVer(Pkg);
if (Ver.end() == false)
{
Expand Down
2 changes: 1 addition & 1 deletion cmdline/apt-config.cc
Expand Up @@ -128,7 +128,7 @@ int main(int argc,const char *argv[]) /*{{{*/
_config->Set(comp + "Name", c->Name);
_config->Set(comp + "Extension", c->Extension);
_config->Set(comp + "Binary", c->Binary);
_config->Set(std::string(comp + "Cost").c_str(), c->Cost);
_config->Set((comp + "Cost").c_str(), c->Cost);
for (std::vector<std::string>::const_iterator a = c->CompressArgs.begin(); a != c->CompressArgs.end(); ++a)
_config->Set(comp + "CompressArg::", *a);
for (std::vector<std::string>::const_iterator a = c->UncompressArgs.begin(); a != c->UncompressArgs.end(); ++a)
Expand Down
5 changes: 1 addition & 4 deletions ftparchive/writer.cc
Expand Up @@ -477,10 +477,7 @@ bool PackagesWriter::DoPackage(string FileName)

// This lists all the changes to the fields we are going to make.
std::vector<pkgTagSection::Tag> Changes;

std::string Size;
strprintf(Size, "%llu", (unsigned long long) FileSize);
Changes.push_back(pkgTagSection::Tag::Rewrite("Size", Size));
Changes.push_back(pkgTagSection::Tag::Rewrite("Size", std::to_string(FileSize)));

for (HashStringList::const_iterator hs = Db.HashesList.begin(); hs != Db.HashesList.end(); ++hs)
{
Expand Down
2 changes: 1 addition & 1 deletion methods/basehttp.cc
Expand Up @@ -191,7 +191,7 @@ bool RequestState::HeaderLine(string const &Line) /*{{{*/
; // we got the expected filesize which is all we wanted
else if (sscanf(Val.c_str(),"bytes %llu-%*u/%llu",&StartPos,&TotalFileSize) != 2)
return _error->Error(_("The HTTP server sent an invalid Content-Range header"));
if ((unsigned long long)StartPos > TotalFileSize)
if (StartPos > TotalFileSize)
return _error->Error(_("This HTTP server has broken range support"));

// figure out what we will download
Expand Down
2 changes: 1 addition & 1 deletion test/libapt/getlistoffilesindir_test.cc
Expand Up @@ -11,7 +11,7 @@

#include "file-helpers.h"

#define P(x) std::string(tempdir).append("/").append(x)
#define P(x) tempdir + "/" + x

TEST(FileUtlTest,GetListOfFilesInDir)
{
Expand Down
4 changes: 2 additions & 2 deletions test/libapt/globalerror_test.cc
Expand Up @@ -111,11 +111,11 @@ TEST(GlobalErrorTest,LongMessage)
longText.append("a");
EXPECT_FALSE(e.Error("%s horrible %s %d times", longText.c_str(), "happened", 2));
EXPECT_TRUE(e.PopMessage(text));
EXPECT_EQ(std::string(longText).append(" horrible happened 2 times"), text);
EXPECT_EQ(longText + " horrible happened 2 times", text);

EXPECT_FALSE(e.Errno("errno", "%s horrible %s %d times", longText.c_str(), "happened", 2));
EXPECT_TRUE(e.PopMessage(text));
EXPECT_EQ(std::string(longText).append(" horrible happened 2 times - errno (0: ").append(textOfErrnoZero).append(")"), text);
EXPECT_EQ(longText + " horrible happened 2 times - errno (0: " + textOfErrnoZero + ")", text);

EXPECT_FALSE(e.Error("%s horrible %s %d times", longText.c_str(), "happened", 2));
std::ostringstream out;
Expand Down

0 comments on commit 1adcf56

Please sign in to comment.