簡體   English   中英

這個語法 *((unsigned int *)(buffer+i)) 在 C 中是什么意思

[英]What does this syntax *((unsigned int *)(buffer+i)) mean in C

這是代碼:

char *command, *buffer;

command = (char *) malloc(200);
bzero(command, 200);

strcpy(command, "./notesearch \'");
buffer = command + strlen(command);
for(int i=0; i < 160; i+=4) {
    *((unsigned int *)(buffer+i)) = ret; // What does this syntax mean?
}

您可以在此處獲取完整代碼 => https://raw.githubusercontent.com/intere/hacking/master/booksrc/exploit_notesearch.c

請幫助我,我是初學者。

從內到外閱讀它。 這里我們必須假設buffer是指向某個 memory 區域或數組元素的指針。 你有:

  • buffer + 1 ==> 到下一個 memory position 或下一個數組元素的地址
  • (unsigned int *)(buffer+i) ==> 將結果指針轉換為unsigned int類型的指針。
  • *((unsigned int *)(buffer+i)) ==> 取消引用unsigned int指出的(獲取值)。
  • *((unsigned int *)(buffer+i)) = ret; ==> 將值賦給變量ret

在 C 中,計算表達式時,始終從內到外 go。

這會將unsigned int ret寫入地址buffer+i

*((unsigned int *)(buffer+i)) = ret
  • buffer+i是一個char* (指向char的指針)
  • (unsigned int *) (unsigned int *)(buffer+i)中的 (unsigned int *) 將指向 char 的指針轉換為指向unsigned int的指針。 這稱為演員表
  • 最后*取消引用指向unsigned int的指針並將ret寫入該地址。

請注意,根據您的硬件架構,這可能會因為對齊問題而失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM