farmersrice's blog

By farmersrice, history, 3 years ago, In English

http://codeforces.com/contest/963/submission/37451734

Not sure where my code has a bug, but I submitted 3 times and it's always the same error on test 48.

I could not decipher this part of the message:

Diagnostics detected issues [cpp.clang++-diagnose]: =================================================================
==6068==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x14e00978 at pc 0x00dc63fd bp 0x1355f754 sp 0x1355f750
READ of size 4 at 0x14e00978 thread T0

I would appreciate any help. Thanks!

 
 
 
 
  • Vote: I like it
  • +5
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hmm, I fixed some possible string out of bounds issues but it's still failing

http://codeforces.com/contest/963/submission/37452849

»
3 years ago, # |
  Vote: I like it +16 Vote: I do not like it

Is your address 6969 or something that you need Address Sanitizer

»
3 years ago, # |
  Vote: I like it +10 Vote: I do not like it

Subtracting k from possible.size() can do bad things, since possible.size() is an unsigned int.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It worked! Thanks!

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it +2 Vote: I do not like it

      "#define SZ(x) ((int)(x).size())" and you're good to go never needing to care about these stupid unsigned sizes again

»
3 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

37476557

Here is the Problem

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it
  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Local arrays with variable length are illegal in C++.

    int n = 5;
    char c[n]; // Compilation error
    
    const int n = 5;
    char c[n]; // OK
    

    But in G++11 it's sometimes correct on small lengths. Two key words there are sometimes and small. I can't surely say is it UB or not, but it's surely wrong C++ code.

    Three options: 1) Use global array with big enought const length;
    2) Use stl-vector;
    3) Use new-statement to construct array.

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it +26 Vote: I do not like it

      Variable length arrays are an extension supported by GCC.

      I'd like to point out a possible misunderstanding of what a language extension is. Compiler authors don't just go like "here's an extension, and it sometimes works, but sometimes doesn't so take care". If it's supported by the compiler, it should work as documented for the intended uses, same as all other language features do.

      Also, the name of the extension may be confusing. Actually, the memory is allocated at the point of declaration. If we change the vaule of n afterwards, the array itself does not change in any way.

      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it +15 Vote: I do not like it

        Thanks for GCC extensions, now I know something new. Though I still stick with standartized C++.

        But what ASan complains about if it is not non-const length array?

      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it +10 Vote: I do not like it

        However it's JUST an extension and has many bugs. See PR16694. I just hate VLA personally.

        • »
          »
          »
          »
          »
          2 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          There's only 11 issues related to variable-length arrays, and not all of them are bugs? I'd say they are in an OK shape then.

          To me, it looks like most of the actual bugs are triggered by bad user code. Well, that's what bad code does: it does not work, one way or another. This time it's a compiler's fault, but whatever.

          Of course, if you don't like a feature, you don't have to use it. C++ is rich with features, plenty to choose form.

          • »
            »
            »
            »
            »
            »
            2 years ago, # ^ |
              Vote: I like it +23 Vote: I do not like it

            In my opinion the usage of VLA in C++ is exactly an instance of bad user code :). It's not standardized so other compilers may reject it and give me an CE.

            It depends on how to define "bad user code". I (and GCC) believe int a[2][n] is bad user code (unless n is a macro expanding to some constexpr). See PR58646. But in a new programmer's view it may be "perfectly well".

            Once a student really blamed me (problem setter) because his usage of VLA in C++ triggered a compiler bug. I tried to reason with him but he just insisted that his program was "perfectly well". I was irritated.

»
2 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

http://codeforces.com/contest/1041/submission/45163366

Can Somebody Help me with this code.

I dont Know Whats Wrong..

»
13 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

heap-buffer-overflow on address 0x608000000088 at pc 0x00000040702d bp 0x7ffeb9016580 sp 0x7ffeb9016578

This error signifies?

class Solution { public: int smallestCommonElement(vector<vector>& mat) { int y; bool flag=1; for(int i=0;i<mat[i].size();i++) {
y=mat[0][i]; //cout<<i<<endl; //cout<<y<<endl; if(flag==0) { break; // cout<<y<<endl;

}

        for(int j=1;j<mat.size();j++)
        {

           for(int k=0;k<mat[j].size();k++)
           {
              // cout<<mat[j][k]<<" "<<j<<" "<<k<<endl;
               if(y==mat[j][k])
               {
                  // y=mat[j][k];
                   flag=0;
                   break;
               }
               else
               {
                   flag=1;
               }
            }
               if(flag==1)
               {
                   //cout<<y<<endl;
                   break;
               }

        }
    }
    return y;
}

};

»
6 months ago, # |
  Vote: I like it -14 Vote: I do not like it

I'm having the same error: https://codeforces.com/problemset/submission/266/78521764 Any ideas why?

  • »
    »
    6 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Change char children[n]; to string children(n, '0'); (gets ACed). Try and not mingle with C-style strings and C++ strings at the same time.

  • »
    »
    6 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Make children array size n and children[n] = '\0' before assigning it to string. See: 78524312

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

https://codeforces.com/contest/1242/submission/85228209 Can anyone help with this? I am getting runtime error

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It seems after S.erase() in some deep recursive call you have invalid iterator in outer dfs(). I changed set<int> S; for map<int, bool> S; + corresponding management, and it worked.

»
2 months ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

Could someone help me with this? Getting runtime error for the problem: https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/.

class Solution {
public:
    
    vector<int> numSmallerByFrequency(vector<string>& a, vector<string>& b) {
       
       int arr[2001], brr[2001],j,i,n,m,sum[11];
        map<char, int> mp;
        n = a.size();
        m = b.size();
        char c;
        for(i=0;i<n;i++)
        {
            c = a[i][0];
            mp.clear();
            for(auto it: a[i])
            {
                if(it < c)
                {
                    c = it;
                    mp[c]++;
                }
                else if(it == c)
                {
                    mp[c]++;
                }
            }
            arr[i] = mp[c];
        }
        for(i=0;i<m;i++)
        {
            c = b[i][0];
            mp.clear();
            for(auto it: b[i])
            {
                if(it < c)
                {
                    c = it;
                    mp[c]++;
                }
                else if(it == c)
                {
                    mp[c]++;
                }
            }
            brr[i] = mp[c];
        }
        for(i=0;i<=10;i++)
        {
            sum[i] = 0;
        }
        for(i=0;i<m;i++)
        {
            sum[brr[i]]++;
        }
        for(i=9;i>=0;i--)
        {
            sum[i] += sum[i+1];
        }
        vector<int> v;
        for(i=0;i<n;i++)
        {
            v.push_back(sum[arr[i]+1]);
        }
        return v;
    }
};

Getting the following error on LeetCode:

================================================================= ==32==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fffa885d98c at pc 0x00000038c24e bp 0x7fffa8859840 sp 0x7fffa8859838 READ of size 4 at 0x7fffa885d98c thread T0 #5 0x7f6777f3282f (/lib/x86_64-linux-gnu/libc.so.6+0x2082f) Address 0x7fffa885d98c is located in stack of thread T0 at offset 16620 in frame This frame has 9 object(s): [32, 8036) 'arr' [8304, 16308) 'brr' [16576, 16620) 'sum' <== Memory access at offset 16620 overflows this variable [16656, 16704) 'mp' [16736, 16737) 'c' [16752, 16760) '__begin2' [16784, 16792) '__end2' [16816, 16824) '__begin2111' [16848, 16856) '__end2116' HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork (longjmp and C++ exceptions are supported) Shadow bytes around the buggy address: 0x100075103ae0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x100075103af0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x100075103b00: 00 00 00 00 00 00 00 00 00 00 04 f2 f2 f2 f2 f2 0x100075103b10: f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 0x100075103b20: f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 f2 00 00 00 00 =>0x100075103b30: 00[04]f2 f2 f2 f2 00 00 00 00 00 00 f2 f2 f2 f2 0x100075103b40: 01 f2 f8 f2 f2 f2 f8 f2 f2 f2 f8 f2 f2 f2 f8 f3 0x100075103b50: f3 f3 f3 f3 00 00 00 00 00 00 00 00 00 00 00 00 0x100075103b60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x100075103b70: 00 00 00 00 f1 f1 f1 f1 01 f3 f3 f3 00 00 00 00 0x100075103b80: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==32==ABORTING