Try   HackMD

2024q1 Homework1 (lab0)

contributed by < 404allen404 >

開發環境

$ gcc --version
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ lscpu
Architecture:            x86_64
  CPU op-mode(s):        32-bit, 64-bit
  Address sizes:         39 bits physical, 48 bits virtual
  Byte Order:            Little Endian
CPU(s):                  12
  On-line CPU(s) list:   0-11
Vendor ID:               GenuineIntel
  Model name:            Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
    CPU family:          6
    Model:               165
    Thread(s) per core:  2
    Core(s) per socket:  6
    Socket(s):           1
    Stepping:            2
    CPU max MHz:         5000.0000
    CPU min MHz:         800.0000
    BogoMIPS:            5199.98

佇列的程式碼實作

  1. 避免過多的中英文混用,已有明確翻譯詞彙者,例如「鏈結串列」(linked list) 和「佇列」(queue),就使用該中文詞彙,英文則留給變數名稱、人名,或者缺乏通用翻譯詞彙的場景。
  2. 留意資訊科技詞彙翻譯
  3. 改進你的漢語表達。

實作 q_insert_head 函式時,有遇到幾個問題如下:

  • cppcheck 在 new_element->list 旁邊有加小括號時 (1),會誤認為有 Memory leak,但把小括號拿掉後 (2) 就可以成功 commit 了。
    ​​​​// (1)
    ​​​​list_add(&(new_element->list), head);
    ​​​​// (2)
    ​​​​list_add(&new_element->list, head);
    
  • 使用 strcpy 會有漏洞,所以我改成使用 strncpy
    • 因為 strcpy 是直接把字串直接複製到目標地址,但如果要複製的字串的大小大於目標地址能容納的大小,可能會覆蓋到不應該覆蓋的位置,所以 strcpy 這個 function 是有危險性的。但如果使用 strncpy ,我們會知道要傳多大的字串到目標地址,相關來說安全很多。
    • 可以參考 Common vulnerabilities guide for C programmers,了解有哪些常見的漏洞。

實作 q_remove_headq_remove_tail 函式時,return NULL 的條件忘記加上 head->next == head

  if (!head || !head->next || head->next == head)
      return NULL;

避免非必要的項目縮排 (即 * ),以清晰、明確,且流暢的漢語書寫。

queue.h 這個檔案內,關於 q_ascendq_descend 的註解應該把 Remove 修改為 Delete 較恰當?因為需要在這兩個 function 內執行 free ,但教材有提到 Remove 和 Delete 的定義是不同的。

整合網頁伺服器有以下幾點訣竅:

  • console.c 中要記得將 use_linenoiseweb_fd 的 static 關鍵字拿掉,否則無法 extern 。
  • 主要被修改的程式碼是 linenoise.c 內的 line_edit 函式,最重要的是要熟悉 select 的用法,如果有開啟網頁伺服器的功能,要記得把 web_fdstdin_fd 都加進 fd_set 裡面,這樣 select 才會一直檢查這兩個檔案描述符有沒有輸入。
  • 最後有個問題想問老師,我透過 curl 命令對伺服器發出請求的時候,如果沒有接任何的文字在後面 (如下圖),會跳出 Unknown command '.' 這個回應是正常的嗎?
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →