Skip to content

Commit

Permalink
Even more correction of issues raised by CodeQL
Browse files Browse the repository at this point in the history
  • Loading branch information
LRGH committed Mar 18, 2024
1 parent 9645598 commit 57cee8c
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 85 deletions.
3 changes: 0 additions & 3 deletions elfesteem/elf_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,6 @@ def add_name(self, name):
def mod_name(self, idx, name):
name = name_to_bytes(name)
n = self.content[idx:self.content.find(data_null, idx)]
data = self.content
if type(data) != str: data = data.pack()
data = data[:idx]+name+data[idx+len(n):]
dif = len(name) - len(n)
if dif != 0:
for sh in self.parent.shlist:
Expand Down
2 changes: 1 addition & 1 deletion elfesteem/macho/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def add(self, *args, **kargs):
if 'content' in kargs :
nwsh = Section(parent=sectionHeader(parent=self.load),
content=kargs['content'])
if not nwlc.segname==None:
if not nwlc.segname is None:
nwsh.parent.segname = nwlc.segname
self.add(nwlc)
self.add(nwsh)
Expand Down
5 changes: 1 addition & 4 deletions elfesteem/macho/sections.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import struct
from elfesteem.macho.common import *
from elfesteem.cstruct import Constants, CBase, CData, CString, CArray, CStructWithStrTable
from elfesteem.cstruct import Constants, CBase, CString, CArray, CStructWithStrTable
from elfesteem.strpatchwork import StrPatchwork

import sys
Expand Down Expand Up @@ -1092,9 +1092,6 @@ def add_name(self, name):
def mod_name(self, idx, name):
name = name_to_bytes(name)
n = self.content[idx:self.content.find(data_null,idx)]
data = self.content
if type(data) != str: data = data.pack()
data = data[:idx]+name+data[idx+len(n):]
dif = len(name) - len(n)
if dif != 0:
for sh in self.parent.shlist:
Expand Down
2 changes: 1 addition & 1 deletion elfesteem/pe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from elfesteem.cstruct import Constants, CBase, CString, CStruct, CArray
from elfesteem.cstruct import data_null, data_empty
from elfesteem.cstruct import bytes_to_name, name_to_bytes
from elfesteem.cstruct import bytes_to_name
from elfesteem.strpatchwork import StrPatchwork
import struct
import logging
Expand Down
2 changes: 1 addition & 1 deletion elfesteem/rprc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# is used, e.g. a value of 'version' greater than 2 in the header.

import struct
from elfesteem.cstruct import CBase, CData, CStruct, data_null, data_empty
from elfesteem.cstruct import CData, CStruct, data_null, data_empty
from elfesteem.strpatchwork import StrPatchwork

# Section types
Expand Down
2 changes: 0 additions & 2 deletions examples/otool.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#! /usr/bin/env python

import sys, os
import time
import platform

sys.path.insert(1, os.path.abspath(sys.path[0]+'/..'))
from elfesteem import macho_init, macho
from elfesteem.cstruct import data_null, CBase

def print_header(e, **fargs):
print("Mach header")
Expand Down
2 changes: 1 addition & 1 deletion examples/readpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

sys.path.insert(1, os.path.abspath(sys.path[0]+'/..'))
from elfesteem import pe_init, pe
import pprint, struct
import struct

def test_rebuild(e):
bin = str(e)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def test_MD5(assertion):
hashlib.md5(struct.pack('BBBB',116,111,116,111)).hexdigest(),
'MD5')

def open_read(f):
fd = open(f, 'rb')
data = fd.read()
fd.close()
return data

if __name__ == "__main__":
exit_value = 0
print_colored.bold('test_MD5')
Expand Down
22 changes: 11 additions & 11 deletions tests/test_elf_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
__dir__ = os.path.dirname(__file__)

from test_all import run_tests, assertion, hashlib
from test_all import run_tests, assertion, hashlib, open_read
from elfesteem.strpatchwork import StrPatchwork
from elfesteem.elf_init import ELF, log
from elfesteem import elf
Expand Down Expand Up @@ -34,7 +34,7 @@ def test_ELF_empty(assertion):

def test_ELF_invalid(assertion):
try:
e = ELF(open(__dir__+'/binary_input/README.txt', 'rb').read())
e = ELF(open_read(__dir__+'/binary_input/README.txt'))
assertion(0,1, 'Not an ELF')
except ValueError:
pass
Expand All @@ -56,7 +56,7 @@ def test_ELF_creation(assertion):

def test_ELF_small32(assertion):
global log_history
elf_small = open(__dir__+'/binary_input/elf_small.out', 'rb').read()
elf_small = open_read(__dir__+'/binary_input/elf_small.out')
assertion('d5284d5f438e25ef5502a0c1de97d84f',
hashlib.md5(elf_small).hexdigest(),
'Reading elf_small.out')
Expand Down Expand Up @@ -153,7 +153,7 @@ def test_ELF_small32(assertion):
'Find pattern (not existing)')

def test_ELF_small64(assertion):
elf64_small = open(__dir__+'/binary_input/elf64_small.out', 'rb').read()
elf64_small = open_read(__dir__+'/binary_input/elf64_small.out')
assertion('dc21d928bb6a3a0fa59b17fafe803d50',
hashlib.md5(elf64_small).hexdigest(),
'Reading elf64_small.out')
Expand All @@ -177,7 +177,7 @@ def test_ELF_small64(assertion):
'Display Reloc Table (elf64)')

def test_ELF_group(assertion):
elf_group = open(__dir__+'/binary_input/elf_cpp.o', 'rb').read()
elf_group = open_read(__dir__+'/binary_input/elf_cpp.o')
assertion('57fed5de9474bc0600173a1db5ee6327',
hashlib.md5(elf_group).hexdigest(),
'Reading elf_cpp.o')
Expand All @@ -193,7 +193,7 @@ def test_ELF_group(assertion):
'Display Group Section')

def test_ELF_TMP320C6x(assertion):
elf_tmp320c6x = open(__dir__+'/binary_input/notle-tesla-dsp.xe64T', 'rb').read()
elf_tmp320c6x = open_read(__dir__+'/binary_input/notle-tesla-dsp.xe64T')
assertion('fb83ed8d809f394e70f5d84d0c8e593f',
hashlib.md5(elf_tmp320c6x).hexdigest(),
'Reading notle-tesla-dsp.xe64T')
Expand Down Expand Up @@ -281,7 +281,7 @@ def test_ELF_wordsize_endianess(assertion):

def test_ELF_tiny84(assertion):
global log_history
elf_tiny = open(__dir__+'/binary_input/tiny84.bin', 'rb').read()
elf_tiny = open_read(__dir__+'/binary_input/tiny84.bin')
assertion('90f9fa06566389883d82b9cda016b10d',
hashlib.md5(elf_tiny).hexdigest(),
'Reading tiny84')
Expand All @@ -297,7 +297,7 @@ def test_ELF_tiny84(assertion):

def test_ELF_tiny76(assertion):
global log_history
elf_tiny = open(__dir__+'/binary_input/tiny76.bin', 'rb').read()
elf_tiny = open_read(__dir__+'/binary_input/tiny76.bin')
assertion('3a5753c93c492d2d1d3fc6c227baec7a',
hashlib.md5(elf_tiny).hexdigest(),
'Reading tiny76')
Expand All @@ -313,7 +313,7 @@ def test_ELF_tiny76(assertion):

def test_ELF_tiny64(assertion):
global log_history
elf_tiny = open(__dir__+'/binary_input/tiny64.bin', 'rb').read()
elf_tiny = open_read(__dir__+'/binary_input/tiny64.bin')
assertion('0dd8a6325f7cf633ed8c527add5dc634',
hashlib.md5(elf_tiny).hexdigest(),
'Reading tiny64')
Expand All @@ -330,7 +330,7 @@ def test_ELF_tiny64(assertion):

def test_ELF_tiny52(assertion):
global log_history
elf_tiny = open(__dir__+'/binary_input/tiny52.bin', 'rb').read()
elf_tiny = open_read(__dir__+'/binary_input/tiny52.bin')
assertion('18ddd4966cb003b80862735d19ddbeb7',
hashlib.md5(elf_tiny).hexdigest(),
'Reading tiny52')
Expand All @@ -348,7 +348,7 @@ def test_ELF_tiny52(assertion):

def test_ELF_tiny45(assertion):
global log_history
elf_tiny = open(__dir__+'/binary_input/tiny45.bin', 'rb').read()
elf_tiny = open_read(__dir__+'/binary_input/tiny45.bin')
assertion('44023f74799f2e009a1400c74de50cdd',
hashlib.md5(elf_tiny).hexdigest(),
'Reading tiny45')
Expand Down
Loading

0 comments on commit 57cee8c

Please sign in to comment.