Skip to content

Commit 88b6765

Browse files
fmartianakleshchev
authored andcommitted
Generate error if the asset upload is bigger than INT_MAX as recommended by Copilot
1 parent 4153d67 commit 88b6765

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

indra/newview/llviewerassetupload.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@
4545
#include "llappviewer.h"
4646
#include "llviewerstats.h"
4747
#include "llfilesystem.h"
48-
#include "llgesturemgr.h"
49-
#include "llpreviewnotecard.h"
50-
#include "llpreviewgesture.h"
5148
#include "llcoproceduremanager.h"
52-
#include "llthread.h"
5349
#include "llkeyframemotion.h"
5450
#include "lldatapacker.h"
5551
#include "llvoavatarself.h"
@@ -405,6 +401,7 @@ LLSD LLNewFileResourceUploadInfo::exportTempFile()
405401

406402
std::string errorMessage;
407403
std::string errorLabel;
404+
std::error_code ec;
408405

409406
bool error = false;
410407

@@ -475,30 +472,38 @@ LLSD LLNewFileResourceUploadInfo::exportTempFile()
475472
error = true;
476473

477474
// read from getFileName()
478-
LLAPRFile infile;
479-
infile.open(getFileName(),LL_APR_RB);
480-
if (!infile.getFileHandle())
475+
LLFile infile(getFileName(), LLFile::in | LLFile::binary, ec);
476+
if (ec || !infile)
481477
{
482478
LL_WARNS() << "Couldn't open file for reading: " << getFileName() << LL_ENDL;
483479
errorMessage = llformat("Failed to open animation file %s\n", getFileName().c_str());
484480
}
485481
else
486482
{
487-
S32 size = (S32)LLFile::size(getFileName());
483+
S64 size = infile.size(ec);
484+
if (ec || size <= 0)
485+
{
486+
LLError::LLUserWarningMsg::showMissingFiles();
487+
LL_ERRS() << "Invalid file" << LL_ENDL;
488+
}
489+
else if (size > INT_MAX)
490+
{
491+
LL_ERRS() << "File is to big, size: " << size << LL_ENDL;
492+
}
488493
U8* buffer = new(std::nothrow) U8[size];
489494
if (!buffer)
490495
{
491496
LLError::LLUserWarningMsg::showOutOfMemory();
492497
LL_ERRS() << "Bad memory allocation for buffer, size: " << size << LL_ENDL;
493498
}
494-
S32 size_read = infile.read(buffer,size);
495-
if (size_read != size)
499+
S64 size_read = infile.read(buffer, size, ec);
500+
if (ec || size_read != size)
496501
{
497502
errorMessage = llformat("Failed to read animation file %s: wanted %d bytes, got %d\n", getFileName().c_str(), size, size_read);
498503
}
499504
else
500505
{
501-
LLDataPackerBinaryBuffer dp(buffer, size);
506+
LLDataPackerBinaryBuffer dp(buffer, (S32)size);
502507
LLKeyframeMotion *motionp = new LLKeyframeMotion(getAssetId());
503508
motionp->setCharacter(gAgentAvatarp);
504509
if (motionp->deserialize(dp, getAssetId(), false))
@@ -544,18 +549,17 @@ LLSD LLNewFileResourceUploadInfo::exportTempFile()
544549
setAssetType(assetType);
545550

546551
// copy this file into the cache for upload
547-
S32 file_size;
548-
LLAPRFile infile;
549-
infile.open(filename, LL_APR_RB, NULL, &file_size);
550-
if (infile.getFileHandle())
552+
LLFile infile(filename, LLFile::in | LLFile::binary, ec);
553+
if (!ec && infile.size(ec) > 0)
551554
{
552555
LLFileSystem file(getAssetId(), assetType, LLFileSystem::APPEND);
553556

557+
S64 read_bytes;
554558
const S32 buf_size = 65536;
555559
U8 copy_buf[buf_size];
556-
while ((file_size = infile.read(copy_buf, buf_size)))
560+
while (((read_bytes = infile.read(copy_buf, buf_size, ec))) > 0)
557561
{
558-
file.write(copy_buf, file_size);
562+
file.write(copy_buf, (S32)read_bytes);
559563
}
560564
}
561565
else
@@ -569,7 +573,6 @@ LLSD LLNewFileResourceUploadInfo::exportTempFile()
569573
}
570574

571575
return LLSD();
572-
573576
}
574577

575578
//=========================================================================

0 commit comments

Comments
 (0)