Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找我的代码中的漏洞(拆分方法)_Python_Security - Fatal编程技术网

Python 查找我的代码中的漏洞(拆分方法)

Python 查找我的代码中的漏洞(拆分方法),python,security,Python,Security,在这里,我尝试在Python中重新创建str.split()方法。我已经尝试并测试了这段代码,它运行良好,但我正在寻找要更正的漏洞。如果有,一定要检查并给出反馈。 编辑:很抱歉不清楚,我想问你们一些代码不起作用的例外情况。我还试图想出一种不看源代码的更精细的方法 def splitt(string,split_by = ' '): output = [] x = 0 for i in range(string.count(split_by)): outpu

在这里,我尝试在Python中重新创建
str.split()
方法。我已经尝试并测试了这段代码,它运行良好,但我正在寻找要更正的漏洞。如果有,一定要检查并给出反馈。 编辑:很抱歉不清楚,我想问你们一些代码不起作用的例外情况。我还试图想出一种不看源代码的更精细的方法

def splitt(string,split_by = ' '):
    output = []
    x = 0
    for i in range(string.count(split_by)):
        output.append((string[x:string.index(split_by,x+1)]).strip())
        x = string.index(split_by,x+1)
    output.append((((string[::-1])[:len(string)-x])[::-1]).strip())

    return output

事实上,您的代码存在一些问题:

  • 通过从
    x+1
    进行搜索,您可能会错过字符串开头出现的
    split_by
    ,导致
    索引在上一次迭代中失败
  • 您调用
    索引的次数超出了需要
  • strip
    仅当分隔符是空白时才有意义,即使这样也可能会删除超出预期的内容,例如在拆分行时删除尾随空格
  • 相反,将
    len(split_by)
    添加到偏移量,以便下次调用
    index
  • 无需在最后一步中反转字符串两次
这将解决这些问题:

def splitt(string,split_by=' '):
    output = []
    x = 0
    for i in range(string.count(split_by)):
        x2 = string.index(split_by, x)
        output.append((string[x:x2]))
        x = x2 + len(split_by)
    output.append(string[x:])
    return output

你说的“漏洞”是什么意思?函数崩溃的极端情况?我也想知道你的意思。术语“漏洞”通常指的是安全问题,而不是一般的编程错误(尽管许多漏洞可能会导致漏洞,具体取决于错误代码的使用位置)。很抱歉,各位直言,我指的是代码不能按预期工作的异常,或者更有效的方法来重新创建split方法