Erlang中有意思的bug

 

代码中常有一些很搞笑的bug,如下面的一行代码被调用两次(Erlang beam)

commit f667e4a47b07b07ed035073b94d699ff5fe0ba9b
Author: Jovi Zhang <[email protected]>
Date:   Fri Dec 2 16:19:22 2011 +0100

    erts: Remove duplicate erts_unblock_fpe
    
    The second function erts_unblock_fpe is not needed in here.

diff --git a/erts/emulator/beam/io.c b/erts/emulator/beam/io.c
index c2cc035..759621d 100644
--- a/erts/emulator/beam/io.c
+++ b/erts/emulator/beam/io.c
@@ -644,7 +644,6 @@ erts_open_driver(erts_driver_t* driver,     /* Pointer to driver. */
                                    name, opts);
        erts_unblock_fpe(fpe_was_unmasked);
        port->caller = NIL;
-       erts_unblock_fpe(fpe_was_unmasked);
        if (IS_TRACED_FL(port, F_TRACE_SCHED_PORTS)) {
            trace_sched_ports_where(port, am_out, am_start);
        }
 

已经unblock了一次,结果又unblock了一次,应该是代码作者的粗心导致的,有意思的是这行代码在erlang的git初始版本中就有了,存活了3年甚至更久,而且经过无数个人的review竟也没有发现。

 

在Linux kernel中也有这样的低级错误,记得以前看见过在pipe.c中的一个patch,作者本意是a=b,结果写成了b=a,那个错误存在了十几年,为什么没有被发现呢?因为那个代码分支一般很难跑进去!

commit e5953cbdff26f7cbae7eff30cd9b18c4e19b7594
Author: Nicolas Kaiser <[email protected]>
Date:   Thu Oct 21 14:56:00 2010 +0200

    pipe: fix failure to return error code on ->confirm()
    
    The arguments were transposed, we want to assign the error code to
    'ret', which is being returned.
    
    Signed-off-by: Nicolas Kaiser <[email protected]>
    Cc: [email protected]
    Signed-off-by: Jens Axboe <[email protected]>

diff --git a/fs/pipe.c b/fs/pipe.c
index 279eef9..37eb1eb 100644
--- a/fs/pipe.c
+++ b/fs/pipe.c
@@ -382,7 +382,7 @@ pipe_read(struct kiocb *iocb, const struct iovec *_iov,
                        error = ops->confirm(pipe, buf);
                        if (error) {
                                if (!ret)
-                                       error = ret;
+                                       ret = error;
                                break;
                        }
 

结论:仔细review你的代码,尽管你写了无数行的代码,尽管你已是一个公认的技术专家,你的代码中仍有可能存在着一些非常低级的bug.

 

 

你可能感兴趣的:(erlang)