@@ -83,7 +83,6 @@ OffloadTargetInfo::OffloadTargetInfo(const StringRef Target,
8383 const OffloadBundlerConfig &BC)
8484 : BundlerConfig(BC) {
8585
86- // TODO: Add error checking from ClangOffloadBundler.cpp
8786 // <kind>-<triple>[-<target id>[:target features]]
8887 // <triple> := <arch>-<vendor>-<os>-<env>
8988 SmallVector<StringRef, 6 > Components;
@@ -1514,6 +1513,9 @@ Error OffloadBundler::UnbundleFiles() {
15141513 StringMap<StringRef> Worklist;
15151514 auto Output = BundlerConfig.OutputFileNames .begin ();
15161515 for (auto &Triple : BundlerConfig.TargetNames ) {
1516+ if (!checkOffloadBundleID (Triple))
1517+ return createStringError (errc::invalid_argument,
1518+ " invalid bundle id from bundle config" );
15171519 Worklist[Triple] = *Output;
15181520 ++Output;
15191521 }
@@ -1533,6 +1535,9 @@ Error OffloadBundler::UnbundleFiles() {
15331535
15341536 StringRef CurTriple = **CurTripleOrErr;
15351537 assert (!CurTriple.empty ());
1538+ if (!checkOffloadBundleID (CurTriple))
1539+ return createStringError (errc::invalid_argument,
1540+ " invalid bundle id read from the bundle" );
15361541
15371542 auto Output = Worklist.begin ();
15381543 for (auto E = Worklist.end (); Output != E; Output++) {
@@ -1591,6 +1596,8 @@ Error OffloadBundler::UnbundleFiles() {
15911596 return createFileError (E.second , EC);
15921597
15931598 // If this entry has a host kind, copy the input file to the output file.
1599+ // We don't need to check E.getKey() here through checkOffloadBundleID
1600+ // because the entire WorkList has been checked above.
15941601 auto OffloadInfo = OffloadTargetInfo (E.getKey (), BundlerConfig);
15951602 if (OffloadInfo.hasHostKind ())
15961603 OutputFile.write (Input.getBufferStart (), Input.getBufferSize ());
@@ -1820,6 +1827,10 @@ Error OffloadBundler::UnbundleArchive() {
18201827 // archive.
18211828 while (!CodeObject.empty ()) {
18221829 SmallVector<StringRef> CompatibleTargets;
1830+ if (!checkOffloadBundleID (CodeObject)) {
1831+ return createStringError (errc::invalid_argument,
1832+ " Invalid bundle id read from code object" );
1833+ }
18231834 auto CodeObjectInfo = OffloadTargetInfo (CodeObject, BundlerConfig);
18241835 if (getCompatibleOffloadTargets (CodeObjectInfo, CompatibleTargets,
18251836 BundlerConfig)) {
@@ -1901,3 +1912,11 @@ Error OffloadBundler::UnbundleArchive() {
19011912
19021913 return Error::success ();
19031914}
1915+
1916+ bool clang::checkOffloadBundleID (const llvm::StringRef Str) {
1917+ // <kind>-<triple>[-<target id>[:target features]]
1918+ // <triple> := <arch>-<vendor>-<os>-<env>
1919+ SmallVector<StringRef, 6 > Components;
1920+ Str.split (Components, ' -' , /* MaxSplit=*/ 5 );
1921+ return Components.size () == 5 || Components.size () == 6 ;
1922+ }
0 commit comments