For-loop Syntax Error in Sqlite3.c - c

cppcheck has determined that the following statement produces a syntax error in sqlite3.c:
for(i=0; i<db->nDb; i++){
Full function:
SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
int i;
Btree *p;
assert( sqlite3_mutex_held(db->mutex) );
for(i=0; i<db->nDb; i++){
p = db->aDb[i].pBt;
if( p && p->sharable ){
assert( p->wantToLock>0 );
p->wantToLock--;
if( p->wantToLock==0 ){
unlockBtreeMutex(p);
}
}
}
}
I do not see how it is a syntax error. Please explain. Is this a false positive?

Looks like a false positive, however I can't reproduce it using Cppcheck 1.48 and C source code for SQLite 3.7.6.3.
If you're using different source or a different version, please log it as a bug.

CppCheck may be parsing the comparison expression incorrectly.
Try adding some spaces or parenthesis to help out,
Original:
i<db->nDb
Modified:
i < db->nDb
This is just my guess.

Related

How to solve error 6011 when cheking for-loop condition?

I am new in C and tried checking the loop condition as to find on the internet, but I get this error I am not able to solve (no other questions/answers were helpful):
void main() {
char* insert = malloc(30);
printf("Insert a Molecular Formula:\n");
gets(insert);
if (insert) {
for (int i = 0; insert[i] != '\0'; i++) {
}
} }
I get the error 6011 in VS inside the for-loop when checking insert[i] != '\0'.
I haven't found a good fix, I have tried cheking return of malloc like if(!insert){ //above code here}
but this didn't help.
Thanks in advance.
Error C6011 is a warning, not an error, so your code will run, but it's not bad to handle these issues if Visual Studio is indicating them.
To get the warning to go away, fix your loop like so:
if (insert)
{
for (int i = 0; insert[i] != '\0'; i++) {
}
}

flex warning multi charecter charecter const

edit: it was indeed the check for '\r\n' it confused me because the line of the error was the counter and not the "if"
I have a weird problem while working with flex,
in this code i'm trying to count how much lines a comment last and for this i change a counter inside an "if" statement
void commentHandler(int line){
int counter = 0;
if (1 == line){
printf("%d COMMENT 1\n",yylineno);
}
else{
for(int i=2 ; i< yyleng-2 ; i++){
if('/' ==yytext[i] && '*' == yytext[i+1]){
errorHandler(ERROR_NESTED_COM);
}
if('\n' == yytext[i] || '\r\n' == yytext[i]){
counter++;
}
}
printf("%d COMMENT %d\n", yylineno - counter, counter + 1);
}
}
but when i compile it it shows the following warning:
warning: multi-charcter charecter constant [-Wmultichar]
as this is my HW i can't ignore warnings but no matter what i put in there it shows the same warning which i never seen before and i don't really know what went wrong.
is this some wrapping of flex that contradicts with it? I played with the name of counter but it didn't change.
what does the warning means?
You need to test characters individually, not in aggregate, but in this case all you really need is:
if ('\n' == yytext[i]) {
As rici points out, the \r check is actually extraneous.

Segmentation error, what is missing in my code?

I'm trying to get my code to convert a text file with 3 columns, xcoor, ycoor, and a symbol with 2 characters into a 30x30 map that prints the 2nd character of the symbol with the rest of the spaces being filled with a '.' However, my code doesn't seem to run, and I get a segmentation error when I try inputting the text file, what am I doing wrong? Thanks in advance
int main(void)
{
char grid[30][30];
for(int i=0;i<30;i++){
for(int j=0;j<30;j++){
grid[i][j]='.';
}
}
int xcoor,ycoor;
char symbol[2];
while((xcoor!=0)||(scanf("%d",&xcoor)))
{
while(xcoor==0){
scanf("%d",&xcoor);
}
scanf("%d %c%c",&ycoor,&symbol[0],&symbol[1]);
grid[xcoor-1][ycoor-1]=symbol[1];
}
for(int i=0;i<30;i++){
for(int j=0;j<30;j++){
printf("%c ",grid[i][j]);
}
printf("\n");
}
return 0;
}
This may not cover ALL of your errors, but immediately I see this:
int xcoor,ycoor;
char symbol[2];
while((xcoor!=0)
Do you think xcoor has a valid value right now? Should it? Because it doesn't. You've created a variable, then before actually setting it to anything, you are checking its value.
It's more than likely your scanf call that's giving you trouble. Regardless, try actually setting these variables. It will most likely fix your issues.
See here for more info: Is reading from unallocated memory safe?
You are using an uninitialized variable xcoor in the conditional of the while statement.
You can fix that by initializing xcoor.
More importantly, you can simplify the code for reading user data and the related error checks. Here's what I suggest:
while ( scanf("%d%d %c%c", &xcoor, &ycoor, &symbol[0], &symbol[1]) == 4 )
{
if ( xcoor < 0 || xcoor >= 30 )
{
// Deal with problem.
fprintf(stderr, "Out or range value of xcoor: %d\n", xcoor);
exit(1);
}
if ( ycoor < 0 || ycoor >= 30 )
{
// Deal with problem.
fprintf(stderr, "Out or range value of ycoor: %d\n", ycoor);
exit(1);
}
grid[xcoor-1][ycoor-1] = symbol[1];
}

how can i get rid of: warning: value computed is not used [-Wunused-value]

I'm a beginner in programming so I would like a simple answer :)
I have a for loop with multiple conditions, which prints out two arrays. It works, but i get a warning and a red line under for(). Why is it so and how can I avoid it? I'm writing it in C and I use a Geany compiler in Ubuntu. :)
for((i=LEN-1) && (j=1); (i>=LEN-3) && (j<=PODIUM); i-- && j++)
{
printf("%d. koht: %s tulemusega %f\n", j, voist[i], tul[i]);
}
for(i=LEN-1,j=1 ; (i>=LEN-3) && (j<=PODIUM); i--, j++)
EDIT:
It works because this is the correct syntax. you don't need to use and operator to combine two initializations or two increments. you can just use the ,
This warning is because of the return value of (i=LEN-1) && (j=1) which is bot used further.To avoid the warning, try this
int temp;
...
temp = (i=LEN-1) && (j=1);
for(; (i>=LEN-3) && (j<=PODIUM); i-- && j++)
{
....
temp = (i=LEN-1) && (j=1);
}

Weird Syntax Error In C in If-else

I was programming an apache module. In the middle of the programming, I was opening a file, but I got an error while compiling.
32. static int wqb_handler(request_rec* req){
33. // Open and read our requested file
34. const char* p_file = req->filename;
35.
36. FILE* req_file;
37. if((req_file = fopen(p_file,"r"))==NULL){
38. return HTTP_NOT_FOUND;
39. }else{
40. fclose(req_file);
41. }
42. // Required variables
43. const char* content_type_a = "text/html";
44.
45. // Set Headers
46. ap_set_content_type(req,content_type_a);
47. if(req->header_only){
48. return OK;
49. }
50.
51.
52. return OK;
53. }
The problem is in that function, I was checking that was the problem, and I think the problem is the if-else statement, the code is written in C, not in C++.
These are the errors:
C:/wqb/wqb1_apache2.c(43) : error C2143: syntax error : missing ';' in front of 'const'
C:/wqb/wqb1_apache2.c(46) : error C2065: 'content_type_a' : undeclarated identifier
If this is C, and you're not compiling in C99 mode (i.e. with a C89 compiler), remember that all declarations must be directly following the start of a block.
Mixing declarations and code is a C99 feature imported from C++.
It appears you are compiling with a Micrososft Visual Studio Compiler in C mode. Note that William H. Gates III chose to ignore C99 entirely and refuses to update the C implemenation for the third millennium. :-)
Share with the solution of your problem. It will help the others to understand it more quickly.
Improve your knowledge about operators and comments.
You're writing too many unnecessary { and } in the operators.
For example, your code:
for( i = 0; i < N; i++ )
{
printf("Hello");
}
More simple/clear code:
for( i = 0; i < N; i++ )
printf("Hello");
...........................................................................
Your code (original) may look so (It's easier to read and understand.):
static int wqb_handler(request_rec* req)
{
/* Open and read our requested file */
const char* p_file = req -> filename;
FILE* req_file;
if((req_file = fopen(p_file,"r"))==NULL)
return HTTP_NOT_FOUND;
else
fclose(req_file);
/* Required variables */
const char* content_type_a = "text/html";
/* Set Headers */
ap_set_content_type(req,content_type_a);
if(req->header_only)
return 0;
return OK;
}

Resources