C++ 头文件中空引用的分段错误驱动



任何人都可以找出以下代码部分的问题:

private:
     TUcdFileReader& operator = (const TUcdFileReader& r) { Fail; return *((TUcdFileReader *) 0); }
     TUcdFileReader(const TUcdFileReader& r) { Fail; }

这属于我在代码中使用的 c 库 (SNAP) 中的头文件。当我在基于 Linux 的系统中编译代码时,会出现以下警告:

Snap-2.1/glib-core/unicode.h(1678): warning #327: NULL reference is not allowed
                TUcdFileReader& operator = (const TUcdFileReader& r) { Fail; return *((TUcdFileReader *) 0); }

总的来说,这导致我出现如下分段错误:(我怀疑这是因为上述警告)

WARNING: Job died due to SIGSEGV - Invalid memory reference

我不写整个头文件,因为它很长并且会造成混乱,但它是它的地址:http://snap.stanford.edu/snap/doc/snapuser-ref/dd/d90/unicode_8h_source.html在这里,我编写了使用TUcdFileReader的头文件的大部分:

protected:
class TUcdFileReader
{
protected:
    TChA buf;
public:
    TChA comment; // contains '#' and everything after it
protected:
    FILE *f;
    int putBackCh;
    int GetCh() {
        if (putBackCh >= 0) { int c = putBackCh; putBackCh = EOF; return c; }
        return fgetc(f); }
    void PutBack(int c) { Assert(putBackCh == EOF); putBackCh = c; }
    // Returns 'false' iff the EOF was encountered before anything was read.
    bool ReadNextLine() {
        buf.Clr(); comment.Clr();
        bool inComment = false, first = true;
        while (true) {
            int c = GetCh();
            if (c == EOF) return ! first;
            else if (c == 13) {
                c = GetCh(); if (c != 10) PutBack(c);
                return true; }
            else if (c == 10) return true;
            else if (c == '#') inComment = true;
            if (! inComment) buf += char(c);
            else comment += char(c); }
            /*first = false;*/}
private:
    TUcdFileReader& operator = (const TUcdFileReader& r) { Fail; return *((TUcdFileReader *) 0); }
    TUcdFileReader(const TUcdFileReader& r) { Fail; }
public:
    TUcdFileReader() : f(0) { }
    TUcdFileReader(const TStr& fileName) : f(0), putBackCh(EOF) { Open(fileName); }
    void Open(const TStr& fileName) { Close(); f = fopen(fileName.CStr(), "rt"); IAssertR(f, fileName); putBackCh = EOF; }
    void Close() { putBackCh = EOF; if (f) { fclose(f); f = 0; }}
    ~TUcdFileReader() { Close(); }
    bool GetNextLine(TStrV& dest) {
        dest.Clr();
        while (true) {
            if (! ReadNextLine()) return false;
            TStr line = buf; line.ToTrunc();
            if (line.Len() <= 0) continue;
            line.SplitOnAllCh(';', dest, false);
            for (int i = 0; i < dest.Len(); i++) dest[i].ToTrunc();
            return true; }}
    static int ParseCodePoint(const TStr& s) {
        int c; bool ok = s.IsHexInt(true, 0, 0x10ffff, c); IAssertR(ok, s); return c; }
    static void ParseCodePointList(const TStr& s, TIntV& dest, bool ClrDestP = true) { // space-separated list
        if (ClrDestP) dest.Clr();
        TStrV parts; s.SplitOnWs(parts);
        for (int i = 0; i < parts.Len(); i++) {
            int c; bool ok = parts[i].IsHexInt(true, 0, 0x10ffff, c); IAssertR(ok, s);
            dest.Add(c); } }
    static void ParseCodePointRange(const TStr& s, int& from, int &to) { // xxxx or xxxx..yyyy
        int i = s.SearchStr(".."); if (i < 0) { from = ParseCodePoint(s); to = from; return; }
        from = ParseCodePoint(s.GetSubStr(0, i - 1));
        to = ParseCodePoint(s.GetSubStr(i + 2, s.Len() - 1)); }
};

此外,TUcdFileReader的唯一地方被称为:

class TSubcatHelper
    {
    public:
        bool hasCat; TUniChSubCategory subCat;
        TStrH invalidCatCodes;
        TUniChDb &owner;
        TSubcatHelper(TUniChDb &owner_) : owner(owner_) { }
        void ProcessComment(TUniChDb::TUcdFileReader &reader)
        {
....

TUcdFileReader被明确定义为没有赋值或复制构造函数,即

private:
    TUcdFileReader& operator = (const TUcdFileReader& r) 
               { Fail; return *((TUcdFileReader *) 0); }
    TUcdFileReader(const TUcdFileReader& r) { Fail; }

看起来他们注定要失败。

在代码中的某个地方调用它们 - 如果您将它们替换为:

private:
    TUcdFileReader& operator = (const TUcdFileReader& r); //**not implemented** 
    TUcdFileReader(const TUcdFileReader& r);              //**not implemented**  

然后,您会收到链接器错误。最好的解决方法是(如果可能的话)实现它们。

相关内容

  • 没有找到相关文章

最新更新