Skip to content
This repository was archived by the owner on Dec 24, 2025. It is now read-only.

Commit eaf1900

Browse files
authored
Merge pull request #42 from 0xeb/hr
minor mods: use the bool operator on ref_t and co.
2 parents e06d4d0 + 50faf94 commit eaf1900

16 files changed

Lines changed: 160 additions & 170 deletions

idapython.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ void convert_idc_args()
701701

702702
// Get reference to the IDC module (it is imported by init.py)
703703
ref_t py_mod(PyW_TryImportModule(S_IDC_MODNAME));
704-
if ( py_mod != nullptr )
704+
if ( py_mod )
705705
PyObject_SetAttrString(py_mod.o, S_IDC_ARGS_VARNAME, py_args.o);
706706
}
707707

@@ -1133,8 +1133,7 @@ ref_t idapython_plugin_t::get_sys_displayhook()
11331133
ref_t h;
11341134
if ( config.repl_use_sys_displayhook )
11351135
{
1136-
ref_t py_sys(PyW_TryImportModule("sys"));
1137-
if ( py_sys != nullptr )
1136+
if ( ref_t py_sys = ref_t(PyW_TryImportModule("sys")) )
11381137
h = PyW_TryGetAttrString(py_sys.o, "displayhook");
11391138
}
11401139
return h;
@@ -1429,7 +1428,7 @@ bool idapython_plugin_t::_extlang_create_object(
14291428

14301429
// Get a reference to the module
14311430
ref_t py_mod(PyW_TryImportModule(modname));
1432-
if ( py_mod == nullptr )
1431+
if ( !py_mod )
14331432
{
14341433
errbuf->sprnt("Could not import module '%s'!", modname);
14351434
break;
@@ -1439,15 +1438,15 @@ bool idapython_plugin_t::_extlang_create_object(
14391438
ref_t py_res;
14401439
if ( nargs == 1 && args[0].vtype == VT_PVOID )
14411440
py_res = try_create_swig_wrapper(py_mod, clsname, args[0].pvoid);
1442-
if ( py_res != nullptr )
1441+
if ( py_res )
14431442
{
14441443
PyObject_SetAttrString(py_res.o, S_PY_IDCCVT_ID_ATTR, PyLong_FromLong(PY_ICID_OPAQUE));
14451444
}
14461445
else
14471446
{
14481447
// Get the class reference
14491448
ref_t py_cls(PyW_TryGetAttrString(py_mod.o, clsname));
1450-
if ( py_cls == nullptr )
1449+
if ( !py_cls )
14511450
{
14521451
errbuf->sprnt("Could not find class type '%s'!", clsname);
14531452
break;
@@ -1495,7 +1494,7 @@ bool idapython_plugin_t::_extlang_eval_snippet(
14951494
globals,
14961495
globals,
14971496
nullptr));
1498-
ok = result != nullptr && !PyErr_Occurred(); //-V560 is always true: !PyErr_Occurred()
1497+
ok = result && !PyErr_Occurred(); //-V560 is always true: !PyErr_Occurred()
14991498
if ( !ok )
15001499
handle_python_error(errbuf);
15011500
}
@@ -1616,7 +1615,7 @@ bool idapython_plugin_t::_extlang_call_method(
16161615
}
16171616

16181617
ref_t py_method(PyW_TryGetAttrString(py_obj.o, method_name));
1619-
if ( py_method == nullptr || !PyCallable_Check(py_method.o) )
1618+
if ( !py_method || !PyCallable_Check(py_method.o) )
16201619
{
16211620
errbuf->sprnt("The input object does not have a callable method called '%s'", method_name);
16221621
break;
@@ -1628,8 +1627,7 @@ bool idapython_plugin_t::_extlang_call_method(
16281627
// to be converted to an unsigned python long
16291628
if ( streq(method_name, "run") )
16301629
{
1631-
ref_t py_ida_idaapi_mod(PyW_TryImportModule(S_IDA_IDAAPI_MODNAME));
1632-
if ( py_ida_idaapi_mod != nullptr )
1630+
if ( ref_t py_ida_idaapi_mod = ref_t(PyW_TryImportModule(S_IDA_IDAAPI_MODNAME)) )
16331631
{
16341632
if ( is_instance_of(py_obj.o, py_ida_idaapi_mod.o, "plugin_t")
16351633
|| is_instance_of(py_obj.o, py_ida_idaapi_mod.o, "plugmod_t") )
@@ -1665,14 +1663,14 @@ bool idapython_plugin_t::_extlang_get_attr(
16651663
{
16661664
// Get a reference to the module
16671665
ref_t py_mod(PyW_TryImportModule(S_MAIN));
1668-
if ( py_mod == nullptr )
1666+
if ( !py_mod )
16691667
break;
16701668

16711669
// Object specified:
16721670
// - (1) string contain attribute name in the main module
16731671
// - (2) opaque object (we use it as is)
16741672
ref_t py_obj;
1675-
if ( obj != nullptr )
1673+
if ( obj )
16761674
{
16771675
// (1) Get attribute from main module
16781676
if ( obj->vtype == VT_STR )
@@ -1692,7 +1690,7 @@ bool idapython_plugin_t::_extlang_get_attr(
16921690
}
16931691
}
16941692
// Get the attribute reference
1695-
if ( py_obj == nullptr )
1693+
if ( !py_obj )
16961694
break;
16971695
}
16981696
// No object specified:
@@ -1708,17 +1706,17 @@ bool idapython_plugin_t::_extlang_get_attr(
17081706
cvt = CIP_FAILED;
17091707
// Get the class
17101708
newref_t cls(PyObject_GetAttrString(py_obj.o, "__class__"));
1711-
if ( cls == nullptr )
1709+
if ( !cls )
17121710
break;
17131711

17141712
// Get its name
17151713
newref_t name(PyObject_GetAttrString(cls.o, "__name__"));
1716-
if ( name == nullptr )
1714+
if ( !name )
17171715
break;
17181716

17191717
// Convert name object to string object
17201718
newref_t string(PyObject_Str(name.o));
1721-
if ( string == nullptr )
1719+
if ( !string )
17221720
break;
17231721

17241722
// Convert name python string to a C string
@@ -1733,7 +1731,7 @@ bool idapython_plugin_t::_extlang_get_attr(
17331731

17341732
ref_t py_attr(PyW_TryGetAttrString(py_obj.o, attr));
17351733
// No attribute?
1736-
if ( py_attr == nullptr )
1734+
if ( !py_attr )
17371735
{
17381736
cvt = CIP_FAILED;
17391737
break;
@@ -1777,7 +1775,7 @@ bool idapython_plugin_t::_extlang_set_attr(
17771775
{
17781776
// Get a reference to the module
17791777
ref_t py_mod(PyW_TryImportModule(S_MAIN));
1780-
if ( py_mod == nullptr )
1778+
if ( !py_mod )
17811779
break;
17821780
ref_t py_obj;
17831781
if ( obj != nullptr )
@@ -1794,7 +1792,7 @@ bool idapython_plugin_t::_extlang_set_attr(
17941792
py_obj = ref_t();
17951793
}
17961794
// No object to set_attr on?
1797-
if ( py_obj == nullptr )
1795+
if ( !py_obj )
17981796
break;
17991797
}
18001798
else
@@ -1864,7 +1862,7 @@ bool idapython_plugin_t::_cli_execute_line(const char *line)
18641862
// Compile as an expression
18651863
qstring qstr(line);
18661864
newref_t py_code(my_CompileString(insert_encoding_cookie(&qstr), "<string>", Py_eval_input));
1867-
if ( py_code == nullptr || PyErr_Occurred() )
1865+
if ( !py_code || PyErr_Occurred() )
18681866
{
18691867
// Not an expression?
18701868
PyErr_Clear();
@@ -1877,7 +1875,7 @@ bool idapython_plugin_t::_cli_execute_line(const char *line)
18771875
PyObject *py_globals = _get_module_globals();
18781876
newref_t py_result(PyEval_EvalCode(py_code.o, py_globals, py_globals));
18791877

1880-
if ( py_result == nullptr || PyErr_Occurred() ) //-V560 is always false: PyErr_Occurred()
1878+
if ( !py_result || PyErr_Occurred() ) //-V560 is always false: PyErr_Occurred()
18811879
{
18821880
PyErr_Print();
18831881
}
@@ -1927,7 +1925,7 @@ bool idapython_plugin_t::_cli_find_completions(
19271925
PYW_GIL_GET;
19281926

19291927
ref_t py_fc(get_idaapi_attr(S_IDAAPI_FINDCOMPLETIONS));
1930-
if ( py_fc == nullptr )
1928+
if ( !py_fc )
19311929
return false;
19321930

19331931
newref_t py_res(PyObject_CallFunction(py_fc.o, "si", line, x)); //lint !e605 !e1776
@@ -1956,7 +1954,7 @@ bool idapython_plugin_t::_handle_file(
19561954
{
19571955
PYW_GIL_CHECK_LOCKED_SCOPE();
19581956
ref_t py_executor_func(get_idaapi_attr(idaapi_executor_func_name));
1959-
if ( py_executor_func == nullptr )
1957+
if ( !py_executor_func )
19601958
{
19611959
errbuf->sprnt("Could not find %s.%s ?!", S_IDA_IDAAPI_MODNAME, idaapi_executor_func_name);
19621960
return false;
@@ -1989,7 +1987,7 @@ bool idapython_plugin_t::_handle_file(
19891987

19901988
// Failure at this point means the script was interrupted
19911989
bool interrupted = false;
1992-
if ( PyW_GetError(errbuf) || py_ret == nullptr )
1990+
if ( PyW_GetError(errbuf) || !py_ret )
19931991
{
19941992
PyErr_Clear();
19951993
if ( errbuf->empty() )
@@ -2091,7 +2089,7 @@ bool idapython_plugin_t::_check_python_dir()
20912089
void idapython_plugin_t::_prepare_sys_path()
20922090
{
20932091
borref_t path(PySys_GetObject((char *) "path"));
2094-
if ( path == nullptr || !PySequence_Check(path.o) )
2092+
if ( !path || !PySequence_Check(path.o) )
20952093
return;
20962094

20972095
qstring new_path;
@@ -2102,7 +2100,7 @@ void idapython_plugin_t::_prepare_sys_path()
21022100
{
21032101
qstring path_el_utf8;
21042102
newref_t path_el(PySequence_GetItem(path.o, i));
2105-
if ( path_el != nullptr
2103+
if ( path_el
21062104
&& PyUnicode_Check(path_el.o)
21072105
&& PyUnicode_as_qstring(&path_el_utf8, path_el.o) )
21082106
{
@@ -2165,11 +2163,11 @@ bool idapython_plugin_t::_run_init_py()
21652163
contents.resize(effsz);
21662164

21672165
newref_t code(my_CompileString(contents.c_str(), path, Py_file_input));
2168-
if ( code == nullptr )
2166+
if ( !code )
21692167
return false;
21702168

21712169
newref_t result(PyEval_EvalCode(code.o, __main__globals, __main__globals));
2172-
return result != nullptr && !PyErr_Occurred();
2170+
return result && !PyErr_Occurred();
21732171
}
21742172

21752173
//------------------------------------------------------------------------

0 commit comments

Comments
 (0)