Immediately I apologize for the quality of the code, only I study, if you need some more information - please.

A string is changed in a mysterious way after using the mysql query with the already found ones in the comparison function.The function also uses a self-written module on C.

Source line:
delete from blabla_1archive_visits where `date`<'2014-01-10';

Turns into:
deletefromblabla_1archive_visitswhere`date`<'2014-01-10'

And so on.

Function call with padding:
diff2_s=diff2(str_query[:], BD, queryId)
The function where the gag occurs:

Shut up between these prints.
def diff2(str_query, BD, queryId):
        print'pre diff2 \ n3 $$$', str_query,'\ n \ n \ n'
        line=0
        cursor.execute('''SELECT id, query FROM queryTable WHERE database =?''',(BD,))
        rows=cursor.fetchall()
        for row in rows:
            if diff3_ctypes(str_query, row[1])>80 .:
                print'after diff2 \ n4 $$$', str_query,'\ n \ n \ n'
                line=row[0]
                break
            else: continue
        if line: return line
        else: return False

C Module:
#include<stdio.h>
#include<string.h>

float diff3(char str1[], char str2[]) {
    char * token1, * last1;
    char * token2, * last2;
    char * i1[10000];
    char * i2[10000];
    float summ=0;
    float res=0;
    float j1=0, j2=0;
    float x1, x2;

    token1=strtok_r(str1,",=().",&last1);
    while(token1!=NULL) {
        i1[(int) j1]=token1;
        j1=j1 + 1;
        token1=strtok_r(NULL,",=().",&last1);
    }
    token2=strtok_r(str2,",=().",&last2);
    while(token2!=NULL) {
        i2[(int) j2]=token2;
        j2=j2 + 1;
        token2=strtok_r(NULL,",=().",&last2);
    }
    x1=j1;
    x2=j2;
    if(x1<=x2)
    {
        for(float k=0.0;k<x1;k=k + 1)
        {
            if(! strcmp(i1[(int) k], i2[(int) k]))
            {
                summ=summ + 1.0;
            }
        }
        res=summ/x1 * 100.0;
    } else {
        for(float k=0.0;k<x2;k=k + 1)
        {
            if(! strcmp(i1[(int) k], i2[(int) k]))
            {
                summ=summ + 1.0;
            }
        }
        res=summ/x2 * 100.0;
    }
    return res;
}

1 Answers 1

The diff3 function doesn't work for me at all.At least it consistently changes the values ​​that I pass on to it and also returns the NAN instead of the value.

A quick inspection revealed that inside it there is a rewrite of the memory inside the strtok_r function.This function writes values ​​to the pointer passed in the third parameter, but you haven't even initialized it.I suspect that the memory in some miraculous way(and there are no others) falls into your own variable str1 or str2.I did not understand the code further, but I suspect that there may still be problems with writing to variables through pointers.

Understand this function separately, debug it first, regardless of the rest of the code.
  • Thanks, the problem was solved by duplicating the string in diff3.so. – Disney's Thief Feb 25 '14 at 09:20