pycdc/data.h
polfg ba35489dd8 Fix std::bad_cast crash on return-in-if followed by comprehension
In 3.11 a return inside an if/else may fall straight into a sibling
branch. The old code unconditionally consumed the next instruction to
skip a redundant jump; when that instruction was the LOAD_CONST of a
code object feeding a MAKE_FUNCTION (e.g. a list comprehension after
'if not x: return []'), dropping it left MAKE_FUNCTION without its
operand and crashed with std::bad_cast.

Now peek the next instruction and keep it only when it is a LOAD_CONST
of a code object; otherwise preserve the original skip behavior.
Added PycBuffer::pos()/setPos() for safe peeking. decompilation target: 234/239 files, corpus 40/95 (+1, 0 regressions).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 11:49:55 +02:00

67 lines
1.5 KiB
C++

#ifndef _PYC_FILE_H
#define _PYC_FILE_H
#include <cstdio>
#include <ostream>
#ifdef WIN32
typedef __int64 Pyc_INT64;
#else
typedef long long Pyc_INT64;
#endif
class PycData {
public:
PycData() { }
virtual ~PycData() { }
virtual bool isOpen() const = 0;
virtual bool atEof() const = 0;
virtual int getByte() = 0;
virtual void getBuffer(int bytes, void* buffer) = 0;
int get16();
int get32();
Pyc_INT64 get64();
};
class PycFile : public PycData {
public:
PycFile(const char* filename);
~PycFile() { if (m_stream) fclose(m_stream); }
bool isOpen() const override { return (m_stream != 0); }
bool atEof() const override;
int getByte() override;
void getBuffer(int bytes, void* buffer) override;
private:
FILE* m_stream;
};
class PycBuffer : public PycData {
public:
PycBuffer(const void* buffer, int size)
: m_buffer((const unsigned char*)buffer), m_size(size), m_pos(0) { }
~PycBuffer() { }
bool isOpen() const override { return (m_buffer != 0); }
bool atEof() const override { return (m_pos == m_size); }
int getByte() override;
void getBuffer(int bytes, void* buffer) override;
/* Save/restore read position so callers can peek the next instruction. */
int pos() const { return m_pos; }
void setPos(int p) { m_pos = p; }
private:
const unsigned char* m_buffer;
int m_size, m_pos;
};
int formatted_print(std::ostream& stream, const char* format, ...);
int formatted_printv(std::ostream& stream, const char* format, va_list args);
#endif