From 8354b8f77fdfd907f07574b63e910aba09232ade Mon Sep 17 00:00:00 2001 From: Nicolas Dato Date: Tue, 26 Nov 2024 11:50:27 -0300 Subject: first article, safer pointers --- zola/config.toml | 6 ++ zola/content/_index.md | 4 +- zola/content/articles/_index.md | 3 + zola/content/articles/safer-pointers.md | 97 +++++++++++++++++++++++++++++++++ zola/content/blog/_index.md | 3 + zola/static/style.css | 9 ++- zola/templates/base.html | 2 +- zola/templates/section.html | 14 ++++- zola/templates/taxonomy_list.html | 15 +++++ zola/templates/taxonomy_single.html | 28 ++++++++++ 10 files changed, 175 insertions(+), 6 deletions(-) create mode 100644 zola/content/articles/_index.md create mode 100644 zola/content/articles/safer-pointers.md create mode 100644 zola/content/blog/_index.md create mode 100644 zola/templates/taxonomy_list.html create mode 100644 zola/templates/taxonomy_single.html (limited to 'zola') diff --git a/zola/config.toml b/zola/config.toml index 184d40e..8c39678 100644 --- a/zola/config.toml +++ b/zola/config.toml @@ -11,10 +11,16 @@ build_search_index = false default_language = "en" +taxonomies = [ + { name = "Categories", feed = true}, + { name = "Tags", feed = true}, +] + [markdown] # Whether to do syntax highlighting # Theme can be customised by setting the `highlight_theme` variable to a theme supported by Zola highlight_code = true +highlight_theme = "solarized-dark" [extra] # Put all your custom variables here diff --git a/zola/content/_index.md b/zola/content/_index.md index 9fa8bc0..aec76e4 100644 --- a/zola/content/_index.md +++ b/zola/content/_index.md @@ -4,7 +4,9 @@ title = "Home" ## About this site -You can find articles [in the articles section](articles). Also, between 2020 and 2022 I wrote Linux articles for the [Baeldung](https://www.baeldung.com) website, [you can read them here](https://www.baeldung.com/linux/author/nicolasdato). +You can find articles [in the articles section](articles), this is about software development. Also, between 2020 and 2022 I wrote Linux articles for the [Baeldung](https://www.baeldung.com) website, [you can read them here](https://www.baeldung.com/linux/author/nicolasdato). + +And I may write general stuff and thoughts [in the blog section](blog). When I'm confident enought about some never-ending-never-completed side projects, I push them to [my gitweb](https://git.ndato.com). diff --git a/zola/content/articles/_index.md b/zola/content/articles/_index.md new file mode 100644 index 0000000..a4e92e5 --- /dev/null +++ b/zola/content/articles/_index.md @@ -0,0 +1,3 @@ ++++ +title = "Articles" ++++ diff --git a/zola/content/articles/safer-pointers.md b/zola/content/articles/safer-pointers.md new file mode 100644 index 0000000..bbefdad --- /dev/null +++ b/zola/content/articles/safer-pointers.md @@ -0,0 +1,97 @@ ++++ +title = "Safer Pointers" +description = "Following some ideas to make it safer to use pointers in C" +date = 2024-11-26 +authors = ["Nicolás Dato"] + +[taxonomies] +Categories=["Software Development"] +Tags=["C", "Patterns", "Pointers"] ++++ + +## General Idea + +*With great power comes great resposability* + +Pointers are an important feature of C, but at the same time they are prone to error. +To reduce errors, and make them safer to use, I propose the following 4 tips: +1. Encapsulating the *malloc()* and *free()* functions +2. Using *calloc()* in the new alloc function, and making the structure "fail-safe" when everything is zero. +3. Making the new *free* function set the freed pointer to *NULL* +4. Every time a pointer is passed to another function, deciding who *owns* the pointer + +### Example +```C +#include +#include +#include + +struct s { + int i; + char c; + char *s; +}; + +struct s *s_alloc(const char *msg) +{ + struct s *s; + + s = calloc(1, sizeof(*s)); + s->s = strdup(msg); + + return s; +} + +void s_free(struct s **s) +{ + if (s != NULL && *s != NULL) { + free((*s)->s); + free(*s); + *s = NULL; + } +} + +void print_s(const struct s *s) +{ + if (s != NULL) { + printf("%s\n", s->s); + } +} + +int main(int argc, char **argv) +{ + struct s *s; + + s = s_alloc("Hello, world!"); + print_s(s); + s_free(&s); + + return 0; +} +``` + +## Encapsulating malloc and free, and using calloc + +When allocating data structures, or new types, always create a new malloc and free functions for that structure. + +This way, if the structure changes you only need to update one malloc and one free function. Also, this will hide how the structure is supposed to be allocated and freed. + +And using *calloc* (instead of malloc) will automatically set all memory to 0. This can be beneficial if the structure is safe when everything is 0, like pointers. + + +## Setting the pointer to NULL in the new free function + +The new *free* function should invalidate the pointer that was freed. Usually this is setting it to *NULL*. + +This way, it is easy check if the pointer is valid or not (or was freed or not). Also, it prevents the access to a freed section of memory, which sometimes doesn't rise any error but produces unexpected behaviour or security issues. + +For instance, FFmpeg use this method with the [av\_freep()](https://git.ffmpeg.org/gitweb/ffmpeg.git/blob/e3b355c0be85ec47ee8b3d7790ad4c4fa26827c0:/libavutil/mem.h#l378) function. + +## Deciding who owns the pointer + +Owning the pointer means who has the resposibility of freeing it, maybe in that moment or later. Each function or program module working with pointers must know who owns them. I try to keep the owner to whoever allocs it. So if a module creates the pointer, that module owns it. Then if that pointer is passed to another module, it should still be owned by the original module if possible. + +If a function doesn't take the ownership, it should receive a *const* pointer when possible. + +If a function takes the ownership of a pointer, it should receive a pointer-to-pointer and set it to *NULL*. The proposed free function works that way, it must take ownership of the function (to free it) so it sets the pointer to NULL. + diff --git a/zola/content/blog/_index.md b/zola/content/blog/_index.md new file mode 100644 index 0000000..34651ab --- /dev/null +++ b/zola/content/blog/_index.md @@ -0,0 +1,3 @@ ++++ +title = "Blog" ++++ diff --git a/zola/static/style.css b/zola/static/style.css index 55c4dbf..61a853d 100644 --- a/zola/static/style.css +++ b/zola/static/style.css @@ -16,7 +16,7 @@ --cyan: #2aa198; --green: #859900; --header-font-size: 150%; - --left-column-width: 22ex; + --left-column-width: 20ex; --border-width: 1px; --padding: 2ex; --margin: 2ex; @@ -24,7 +24,7 @@ --content-padding-right: 4ex; --body-max-width: 130ex; --body-font-size: 17px; - --first-letter-font-size: 120%; + --first-letter-font-size: 130%; --dt-font-size: 120%; --primary-color: var(--base1); --secondary-color: var(--yellow); @@ -126,6 +126,11 @@ h2 { border-top: 1px solid var(--primary-color); } +pre { + padding: 1ex; + border: var(--border-width) solid var(--primary-border-color); +} + :link, :visited { color: var(--link-text-color); diff --git a/zola/templates/base.html b/zola/templates/base.html index 22a4728..d60212c 100644 --- a/zola/templates/base.html +++ b/zola/templates/base.html @@ -22,9 +22,9 @@
  • Home
  • Articles
  • +
  • Blog
  • Git
  • CV / Resume
  • -
  • Contact
  • Links
    diff --git a/zola/templates/section.html b/zola/templates/section.html index 11e7e6a..71d3e06 100644 --- a/zola/templates/section.html +++ b/zola/templates/section.html @@ -4,7 +4,7 @@ {% block header %}{{ section.title }}{% endblock %} {% block navigation %} {% for a in section.ancestors %} - {% set s = get_section(path=a) %} + {% set s = get_section(path=a) -%} {% if loop.index != 1 %} > {% endif %}{{ s.title }} {% endfor %} > {{ section.title }} @@ -16,7 +16,17 @@

    {{ page.title }}

    -

    {{ page.description }} {{ page.updated }}

    +

    {{ page.date }}: {{ page.description }}

    +

    + {% for kind, values in page.taxonomies %} + {% set cat = get_taxonomy(kind=kind) -%} + {{ kind }}: + {% for v in values %} + {% if loop.index != 1%}, {% endif %}{{ v }} + {% endfor %} +
    + {% endfor %} +

    {% endfor %} diff --git a/zola/templates/taxonomy_list.html b/zola/templates/taxonomy_list.html new file mode 100644 index 0000000..4827853 --- /dev/null +++ b/zola/templates/taxonomy_list.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block title %}{{ taxonomy.name }}{% endblock %} +{% block header %}{{ taxonomy.name }}{% endblock %} +{% block navigation %}Home > {{ taxonomy.name }}{% endblock %} +{% block content %} +
    +{% for term in terms %} +
    +

    {{ term.name }}

    +
    +{% endfor %} +
    +{% endblock %} + diff --git a/zola/templates/taxonomy_single.html b/zola/templates/taxonomy_single.html new file mode 100644 index 0000000..d5641e5 --- /dev/null +++ b/zola/templates/taxonomy_single.html @@ -0,0 +1,28 @@ +{% extends "base.html" %} + +{% block title %}{{ term.name }}{% endblock %} +{% block header %}{{ term.name }}{% endblock %} +{% block navigation %}Home > {{ taxonomy.name }} > {{ term.name }}{% endblock %} +{% block content %} +
    +{% for page in term.pages %} +
    +

    {{ page.title }}

    +
    +
    +

    {{ page.date }}: {{ page.description }}

    +

    + {% for kind, values in page.taxonomies %} + {% set cat = get_taxonomy(kind=kind) -%} + {{ kind }}: + {% for v in values %} + {% if loop.index != 1%}, {% endif %}{{ v }} + {% endfor %} +
    + {% endfor %} +

    +
    +{% endfor %} +
    +{% endblock %} + -- cgit v1.2.3