惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
月光博客
月光博客
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
IT之家
IT之家
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
S
SegmentFault 最新的问题
宝玉的分享
宝玉的分享
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
博客园 - 司徒正美
爱范儿
爱范儿

Last Blog

CMU 15-213 Attack Lab GAMES101 [1] Affine Transformation & Projection CMU 15-213 Bomblab CMU 15-213 [1] Scribe.cx 2 - 对话与持久化存储 关于 X11 与 Wayland 的剪贴板同步问题 关于 X11 与 Wayland 的剪贴板同步问题 Scribe.cx 1 - 跨浏览器的插件侧板控制 Page Assist 2025 年终总结 2025 年终总结 Linuxqq 粘贴板同步问题 关于所谓“配置工程师” 记一次小端序内存的实际体现 CS110L L3 - Memory Safety in Rust CS110L L2 - Program Analysis 在 Neovim(v0.10+) 上使用 arduino-language-server PA3 - Batch Processing System 2024 年终总结 Arcaea 曲名匹配器 Arcaea 曲名匹配器 PA W9 - Linking & Loading Hyprland 二周目 Hyprland 二周目 PA2 Part 2 - Emulated Hardware Device PA W8 - IO Devices Learn C the Hard Way PA2 Part 1 - Instruction Set Implementation & KLIB Learning Makefile with PA 阅读 MQTTX 项目:Protobuf Test Case
CS110L L1 - Safety in System Programming
Last · 2025-04-30 · via Last Blog

  • Learning Advanced Mathematics is just too boring, I need something interesting.

Warning

If someone is reading this blog, please be aware that the writer did not consider the experience of the other readers.
After all, the most important part is about writing things down for better memorization.

Why not C/C++?

  • Severe security problem could happen if C/C++ code is not carefully handled.

  • For example, a classic buffer overflow attack might happen during the runtime of the code below:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    #include <stdio.h>
    #include <string.h>

    int main() {
    char s[100];
    printf("\nEnter a string: ");
    gets(s);

    for (int i = 0; s[i] != '\0'; i++) {

    }

    printf("\nString in Upper Case = %s", s);
    return 0;
    }
  • The general idea of a buffer overflow attack is to fill the buffer out of its intended size, in which case malicious data could grow from low addresses to high addresses, potentially overriding the return address of the currently executing function.

  • The famous Morris Worm virus took advantage of this ‘feature’ of C, and took down thousands of computers back in 1988.


  • You might argue that well nowadays programmers are aware of those holes, and they would handle their code well that does not involve any of these issues. Or is it that ‘Professional engineers don’t make such silly mistakes’ you might consider.

  • Fact is that countless real-world examples have shown that even the big tech company like Google/Microsoft who had invested a lot in security still include many of those ‘simple’ mistakes in their products. Not because they are silly, it’s just that the buffer-overflow issue could happen anywhere and hard to mitigate.

  • See this example below:

    1
    2
    3
    4
    5
    6
    char buffer[128];

    int bytesToCopy = packet.length;
    if (bytesToCopy < 128) {
    strncpy(buffer, packet.data, bytesToCopy);
    }
  • At first glance the code is fine, with proper boundary check and using strncpy instead of strcpy.

  • Turns out the problem in this code lies in the type of variable bytesToCopy: it’s an int type, while strncpy‘s third parameter takes in a size_t type, which is an unsigned integer. If the attacker transfer a packet with a ‘negative length’, the if-check would pass and strncpy would allow for a really large string copy which could eventually overflow the 128-byte buffer created in stack for the packet data.

Why not GC Language?

  • Simple reason: they’re slow.

  • Plus, they’re not necessarily safe as well.

  • Title: CS110L L1 - Safety in System Programming
  • Author: Last
  • Created at : 2025-04-30 22:20:21
  • Link: https://blog.imlast.top/2025/04/30/cs110l-l1/
  • License: This work is licensed under CC BY-NC-SA 4.0.

On this page

CS110L L1 - Safety in System Programming

  1. Why not C/C++?
  2. Why not GC Language?