Fix clang static analyzer issues, as reported by floooh.
This commit is contained in:
parent
1f654e1603
commit
b329715caf
@ -626,6 +626,8 @@ void CompileAndLinkShaders()
|
|||||||
char** shaderStrings = ReadFileData(workItem->name.c_str());
|
char** shaderStrings = ReadFileData(workItem->name.c_str());
|
||||||
if (! shaderStrings) {
|
if (! shaderStrings) {
|
||||||
usage();
|
usage();
|
||||||
|
delete &program;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
|
const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
|
||||||
@ -945,14 +947,14 @@ int fopen_s(
|
|||||||
//
|
//
|
||||||
char** ReadFileData(const char* fileName)
|
char** ReadFileData(const char* fileName)
|
||||||
{
|
{
|
||||||
FILE *in;
|
FILE *in = nullptr;
|
||||||
int errorCode = fopen_s(&in, fileName, "r");
|
int errorCode = fopen_s(&in, fileName, "r");
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
const int maxSourceStrings = 5;
|
const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
|
||||||
char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1));
|
char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
|
||||||
|
|
||||||
if (errorCode) {
|
if (errorCode || in == nullptr) {
|
||||||
printf("Error: unable to open input file: %s\n", fileName);
|
printf("Error: unable to open input file: %s\n", fileName);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@ -962,49 +964,61 @@ char** ReadFileData(const char* fileName)
|
|||||||
|
|
||||||
fseek(in, 0, SEEK_SET);
|
fseek(in, 0, SEEK_SET);
|
||||||
|
|
||||||
char *fdata = (char*)malloc(count+2);
|
char *fdata = (char*)malloc(count+2); // freed before return of this function
|
||||||
if (! fdata) {
|
if (! fdata) {
|
||||||
printf("Error allocating memory\n");
|
printf("Error allocating memory\n");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
if ((int)fread(fdata,1,count, in) != count) {
|
if ((int)fread(fdata, 1, count, in) != count) {
|
||||||
printf("Error reading input file: %s\n", fileName);
|
printf("Error reading input file: %s\n", fileName);
|
||||||
return nullptr;
|
free(fdata);
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
fdata[count] = '\0';
|
fdata[count] = '\0';
|
||||||
fclose(in);
|
fclose(in);
|
||||||
|
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
return_data[0]=(char*)malloc(count+2);
|
// recover from empty file
|
||||||
|
return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
|
||||||
return_data[0][0]='\0';
|
return_data[0][0]='\0';
|
||||||
NumShaderStrings = 0;
|
NumShaderStrings = 0;
|
||||||
return return_data;
|
free(fdata);
|
||||||
} else
|
|
||||||
NumShaderStrings = 1;
|
|
||||||
|
|
||||||
|
return return_data;
|
||||||
|
} else
|
||||||
|
NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
|
||||||
|
|
||||||
|
// compute how to split up the file into multiple strings, for testing multiple strings
|
||||||
int len = (int)(ceil)((float)count/(float)NumShaderStrings);
|
int len = (int)(ceil)((float)count/(float)NumShaderStrings);
|
||||||
int ptr_len=0,i=0;
|
int ptr_len = 0;
|
||||||
while(count>0){
|
int i = 0;
|
||||||
return_data[i]=(char*)malloc(len+2);
|
while (count > 0) {
|
||||||
memcpy(return_data[i],fdata+ptr_len,len);
|
return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
|
||||||
return_data[i][len]='\0';
|
memcpy(return_data[i], fdata + ptr_len, len);
|
||||||
count-=(len);
|
return_data[i][len] = '\0';
|
||||||
ptr_len+=(len);
|
count -= len;
|
||||||
if(count<len){
|
ptr_len += len;
|
||||||
if(count==0){
|
if (count < len) {
|
||||||
NumShaderStrings=(i+1);
|
if (count == 0) {
|
||||||
|
NumShaderStrings = i + 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
len = count;
|
len = count;
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(fdata);
|
||||||
|
|
||||||
return return_data;
|
return return_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FreeFileData(char** data)
|
void FreeFileData(char** data)
|
||||||
{
|
{
|
||||||
for(int i=0;i<NumShaderStrings;i++)
|
for(int i = 0; i < NumShaderStrings; i++)
|
||||||
free(data[i]);
|
free(data[i]);
|
||||||
|
|
||||||
|
free(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InfoLogMsg(const char* msg, const char* name, const int num)
|
void InfoLogMsg(const char* msg, const char* name, const int num)
|
||||||
|
|||||||
@ -817,7 +817,6 @@ int TPpContext::CPPextension(TPpToken* ppToken)
|
|||||||
int TPpContext::readCPPline(TPpToken* ppToken)
|
int TPpContext::readCPPline(TPpToken* ppToken)
|
||||||
{
|
{
|
||||||
int token = scanToken(ppToken);
|
int token = scanToken(ppToken);
|
||||||
bool isVersion = false;
|
|
||||||
|
|
||||||
if (token == CPP_IDENTIFIER) {
|
if (token == CPP_IDENTIFIER) {
|
||||||
if (ppToken->atom == defineAtom) {
|
if (ppToken->atom == defineAtom) {
|
||||||
@ -864,7 +863,6 @@ int TPpContext::readCPPline(TPpToken* ppToken)
|
|||||||
token = CPPerror(ppToken);
|
token = CPPerror(ppToken);
|
||||||
} else if (ppToken->atom == versionAtom) {
|
} else if (ppToken->atom == versionAtom) {
|
||||||
token = CPPversion(ppToken);
|
token = CPPversion(ppToken);
|
||||||
isVersion = true;
|
|
||||||
} else if (ppToken->atom == extensionAtom) {
|
} else if (ppToken->atom == extensionAtom) {
|
||||||
token = CPPextension(ppToken);
|
token = CPPextension(ppToken);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -408,7 +408,7 @@ protected:
|
|||||||
// handle any non-escaped newline
|
// handle any non-escaped newline
|
||||||
if (ch == '\r' || ch == '\n') {
|
if (ch == '\r' || ch == '\n') {
|
||||||
if (ch == '\r' && input->peek() == '\n')
|
if (ch == '\r' && input->peek() == '\n')
|
||||||
ch = input->get();
|
input->get();
|
||||||
return '\n';
|
return '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -116,12 +116,11 @@ int TPpContext::InitScanner()
|
|||||||
int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken)
|
int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken)
|
||||||
{
|
{
|
||||||
bool HasDecimalOrExponent = false;
|
bool HasDecimalOrExponent = false;
|
||||||
int declen, exp, ExpSign;
|
int declen;
|
||||||
int str_len;
|
int str_len;
|
||||||
int isDouble = 0;
|
int isDouble = 0;
|
||||||
|
|
||||||
declen = 0;
|
declen = 0;
|
||||||
exp = 0;
|
|
||||||
|
|
||||||
str_len=len;
|
str_len=len;
|
||||||
char* str = ppToken->name;
|
char* str = ppToken->name;
|
||||||
@ -152,34 +151,32 @@ int TPpContext::lFloatConst(int len, int ch, TPpToken* ppToken)
|
|||||||
HasDecimalOrExponent = true;
|
HasDecimalOrExponent = true;
|
||||||
if (len >= TPpToken::maxTokenLength) {
|
if (len >= TPpToken::maxTokenLength) {
|
||||||
parseContext.error(ppToken->loc, "float literal too long", "", "");
|
parseContext.error(ppToken->loc, "float literal too long", "", "");
|
||||||
len = 1,str_len=1;
|
len = 1;
|
||||||
|
str_len = 1;
|
||||||
} else {
|
} else {
|
||||||
ExpSign = 1;
|
|
||||||
str[len++] = (char)ch;
|
str[len++] = (char)ch;
|
||||||
ch = getChar();
|
ch = getChar();
|
||||||
if (ch == '+') {
|
if (ch == '+') {
|
||||||
str[len++] = (char)ch;
|
str[len++] = (char)ch;
|
||||||
ch = getChar();
|
ch = getChar();
|
||||||
} else if (ch == '-') {
|
} else if (ch == '-') {
|
||||||
ExpSign = -1;
|
|
||||||
str[len++] = (char)ch;
|
str[len++] = (char)ch;
|
||||||
ch = getChar();
|
ch = getChar();
|
||||||
}
|
}
|
||||||
if (ch >= '0' && ch <= '9') {
|
if (ch >= '0' && ch <= '9') {
|
||||||
while (ch >= '0' && ch <= '9') {
|
while (ch >= '0' && ch <= '9') {
|
||||||
if (len < TPpToken::maxTokenLength) {
|
if (len < TPpToken::maxTokenLength) {
|
||||||
exp = exp*10 + ch - '0';
|
|
||||||
str[len++] = (char)ch;
|
str[len++] = (char)ch;
|
||||||
ch = getChar();
|
ch = getChar();
|
||||||
} else {
|
} else {
|
||||||
parseContext.error(ppToken->loc, "float literal too long", "", "");
|
parseContext.error(ppToken->loc, "float literal too long", "", "");
|
||||||
len = 1,str_len=1;
|
len = 1;
|
||||||
|
str_len = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
parseContext.error(ppToken->loc, "bad character in float exponent", "", "");
|
parseContext.error(ppToken->loc, "bad character in float exponent", "", "");
|
||||||
}
|
}
|
||||||
exp *= ExpSign;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user