Parcourir la source

Return std::unique_ptr member as const ref

Returning raw pointer has a risk that it may be stolen by
std::shared_ptr in accident.
Tatsuhiro Tsujikawa il y a 12 ans
Parent
commit
da7400ef5c

+ 1 - 1
src/DefaultBtAnnounce.cc

@@ -69,7 +69,7 @@ DefaultBtAnnounce::DefaultBtAnnounce
     incomplete_(0),
     announceList_(bittorrent::getTorrentAttrs(downloadContext)->announceList),
     option_(option),
-    randomizer_(SimpleRandomizer::getInstance()),
+    randomizer_(SimpleRandomizer::getInstance().get()),
     tcpPort_(0)
 {}
 

+ 1 - 2
src/DefaultPieceStorage.cc

@@ -478,8 +478,7 @@ void DefaultPieceStorage::completePiece(const std::shared_ptr<Piece>& piece)
     }
 #ifdef ENABLE_BITTORRENT
     if(downloadContext_->hasAttribute(CTX_ATTR_BT)) {
-      auto torrentAttrs = bittorrent::getTorrentAttrs(downloadContext_);
-      if(!torrentAttrs->metadata.empty()) {
+      if(!bittorrent::getTorrentAttrs(downloadContext_)->metadata.empty()) {
 #ifdef __MINGW32__
         // On Windows, if aria2 opens files with GENERIC_WRITE access
         // right, some programs cannot open them aria2 is seeding. To

+ 3 - 2
src/DownloadContext.cc

@@ -156,12 +156,13 @@ void DownloadContext::setAttribute
   attrs_[key] = std::move(value);
 }
 
-ContextAttribute* DownloadContext::getAttribute(ContextAttributeType key)
+const std::unique_ptr<ContextAttribute>& DownloadContext::getAttribute
+(ContextAttributeType key)
 {
   assert(key < MAX_CTX_ATTR);
   const std::unique_ptr<ContextAttribute>& attr = attrs_[key];
   if(attr) {
-    return attr.get();
+    return attr;
   } else {
     throw DL_ABORT_EX(fmt("No attribute named %s",
                           strContextAttributeType(key)));

+ 2 - 1
src/DownloadContext.h

@@ -206,7 +206,8 @@ public:
   void setAttribute
   (ContextAttributeType key, std::unique_ptr<ContextAttribute> value);
 
-  ContextAttribute* getAttribute(ContextAttributeType key);
+  const std::unique_ptr<ContextAttribute>& getAttribute
+  (ContextAttributeType key);
 
   bool hasAttribute(ContextAttributeType key) const;
 

+ 5 - 4
src/DownloadEngine.cc

@@ -549,14 +549,15 @@ void DownloadEngine::setAuthConfigFactory
   authConfigFactory_ = std::move(factory);
 }
 
-AuthConfigFactory* DownloadEngine::getAuthConfigFactory() const
+const std::unique_ptr<AuthConfigFactory>&
+DownloadEngine::getAuthConfigFactory() const
 {
-  return authConfigFactory_.get();
+  return authConfigFactory_;
 }
 
-CookieStorage* DownloadEngine::getCookieStorage() const
+const std::unique_ptr<CookieStorage>& DownloadEngine::getCookieStorage() const
 {
-  return cookieStorage_.get();
+  return cookieStorage_;
 }
 
 void DownloadEngine::setRefreshInterval(int64_t interval)

+ 2 - 2
src/DownloadEngine.h

@@ -302,7 +302,7 @@ public:
    uint16_t port,
    const std::string& username);
 
-  CookieStorage* getCookieStorage() const;
+  const std::unique_ptr<CookieStorage>& getCookieStorage() const;
 
 #ifdef ENABLE_BITTORRENT
   const std::shared_ptr<BtRegistry>& getBtRegistry() const
@@ -333,7 +333,7 @@ public:
 
   void setAuthConfigFactory(std::unique_ptr<AuthConfigFactory> factory);
 
-  AuthConfigFactory* getAuthConfigFactory() const;
+  const std::unique_ptr<AuthConfigFactory>& getAuthConfigFactory() const;
 
   void setRefreshInterval(int64_t interval);
 

+ 2 - 2
src/HttpRequestCommand.cc

@@ -98,8 +98,8 @@ createHttpRequest(const std::shared_ptr<Request>& req,
   httpRequest->setFileEntry(fileEntry);
   httpRequest->setSegment(segment);
   httpRequest->addHeader(option->get(PREF_HEADER));
-  httpRequest->setCookieStorage(e->getCookieStorage());
-  httpRequest->setAuthConfigFactory(e->getAuthConfigFactory());
+  httpRequest->setCookieStorage(e->getCookieStorage().get());
+  httpRequest->setAuthConfigFactory(e->getAuthConfigFactory().get());
   httpRequest->setOption(option.get());
   httpRequest->setProxyRequest(proxyRequest);
   httpRequest->setAcceptMetalink(rg->getDownloadContext()->

+ 5 - 0
src/RequestGroupEntry.cc

@@ -52,6 +52,11 @@ RequestGroupEntry::~RequestGroupEntry()
   requestGroup_->decreaseNumCommand();
 }
 
+const std::unique_ptr<Command>& RequestGroupEntry::getNextCommand() const
+{
+  return nextCommand_;
+}
+
 std::unique_ptr<Command> RequestGroupEntry::popNextCommand()
 {
   return std::move(nextCommand_);

+ 1 - 4
src/RequestGroupEntry.h

@@ -60,10 +60,7 @@ public:
     return requestGroup_;
   }
 
-  Command* getNextCommand() const
-  {
-    return nextCommand_.get();
-  }
+  const std::unique_ptr<Command>& getNextCommand() const;
 
   std::unique_ptr<Command> popNextCommand();
 

+ 2 - 2
src/SimpleRandomizer.cc

@@ -45,12 +45,12 @@ namespace aria2 {
 
 std::unique_ptr<SimpleRandomizer> SimpleRandomizer::randomizer_;
 
-SimpleRandomizer* SimpleRandomizer::getInstance()
+const std::unique_ptr<SimpleRandomizer>& SimpleRandomizer::getInstance()
 {
   if(!randomizer_) {
     randomizer_.reset(new SimpleRandomizer());
   }
-  return randomizer_.get();
+  return randomizer_;
 }
 
 void SimpleRandomizer::init()

+ 1 - 1
src/SimpleRandomizer.h

@@ -56,7 +56,7 @@ private:
   SimpleRandomizer();
 public:
 
-  static SimpleRandomizer* getInstance();
+  static const std::unique_ptr<SimpleRandomizer>& getInstance();
 
   static void init();
 

+ 2 - 2
src/SingletonHolder.h

@@ -50,9 +50,9 @@ private:
 public:
   ~SingletonHolder() {}
 
-  static T* instance()
+  static std::unique_ptr<T>& instance()
   {
-    return instance_.get();
+    return instance_;
   }
 
   static void instance(std::unique_ptr<T> ptr)

+ 1 - 2
src/UTMetadataPostDownloadHandler.cc

@@ -59,8 +59,7 @@ bool UTMetadataPostDownloadHandler::Criteria::match
   const std::shared_ptr<DownloadContext>& dctx =
     requestGroup->getDownloadContext();
   if(dctx->hasAttribute(CTX_ATTR_BT)) {
-    auto attrs = bittorrent::getTorrentAttrs(dctx);
-    if(attrs->metadata.empty()) {
+    if(bittorrent::getTorrentAttrs(dctx)->metadata.empty()) {
       return true;
     }
   }

+ 3 - 4
src/aria2api.cc

@@ -737,9 +737,8 @@ struct RequestGroupDH : public DownloadHandle {
   {
 #ifdef ENABLE_BITTORRENT
     if(group->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) {
-      std::shared_ptr<TorrentAttribute> torrentAttrs =
-        bittorrent::getTorrentAttrs(group->getDownloadContext());
-      return torrentAttrs->infoHash;
+      return bittorrent::getTorrentAttrs(group->getDownloadContext())
+        ->infoHash;
     }
 #endif // ENABLE_BITTORRENT
     return A2STR::NIL;
@@ -804,7 +803,7 @@ struct RequestGroupDH : public DownloadHandle {
     BtMetaInfoData res;
 #ifdef ENABLE_BITTORRENT
     if(group->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) {
-      std::shared_ptr<TorrentAttribute> torrentAttrs =
+      auto torrentAttrs =
         bittorrent::getTorrentAttrs(group->getDownloadContext());
       res.announceList = torrentAttrs->announceList;
       res.comment = torrentAttrs->comment;

+ 1 - 1
src/bittorrent_helper.cc

@@ -626,7 +626,7 @@ TorrentAttribute* getTorrentAttrs
 
 TorrentAttribute* getTorrentAttrs(DownloadContext* dctx)
 {
-  return static_cast<TorrentAttribute*>(dctx->getAttribute(CTX_ATTR_BT));
+  return static_cast<TorrentAttribute*>(dctx->getAttribute(CTX_ATTR_BT).get());
 }
 
 const unsigned char* getInfoHash

+ 2 - 1
src/bittorrent_helper.h

@@ -109,7 +109,7 @@ void loadFromMemory(const std::shared_ptr<ValueBase>& torrent,
                     const std::string& overrideName = "");
 
 // Parses BitTorrent Magnet URI and returns
-// std::shared_ptr<TorrentAttribute> which includes infoHash, name and
+// std::unique_ptr<TorrentAttribute> which includes infoHash, name and
 // announceList. If parsing operation failed, an RecoverableException
 // will be thrown.  infoHash and name are string and announceList is a
 // list of list of announce URI.
@@ -149,6 +149,7 @@ void computeFastSet
 (std::vector<size_t>& fastSet, const std::string& ipaddr,
  size_t numPieces, const unsigned char* infoHash, size_t fastSetSize);
 
+// Make sure that don't recieve return value into std::shared_ptr.
 TorrentAttribute* getTorrentAttrs(DownloadContext* dctx);
 TorrentAttribute* getTorrentAttrs
 (const std::shared_ptr<DownloadContext>& dctx);

+ 1 - 1
src/util.cc

@@ -1506,7 +1506,7 @@ std::vector<std::pair<size_t, std::string> > createIndexPaths(std::istream& i)
 namespace {
 void generateRandomDataRandom(unsigned char* data, size_t length)
 {
-  SimpleRandomizer* rd = SimpleRandomizer::getInstance();
+  const auto& rd = SimpleRandomizer::getInstance();
   for(size_t i = 0; i < length; ++i) {
     data[i] = static_cast<unsigned long>(rd->getRandomNumber(256));
   }

+ 0 - 1
test/UTMetadataRequestExtensionMessageTest.cc

@@ -41,7 +41,6 @@ public:
     messageFactory_.reset(new WrapExtBtMessageFactory());
     dispatcher_.reset(new MockBtMessageDispatcher());
     dctx_.reset(new DownloadContext());
-    std::shared_ptr<TorrentAttribute> attrs(new TorrentAttribute());
     dctx_->setAttribute(CTX_ATTR_BT, make_unique<TorrentAttribute>());
     peer_.reset(new Peer("host", 6880));
     peer_->allocateSessionResource(0, 0);