summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--zola/config.toml6
-rw-r--r--zola/content/_index.md4
-rw-r--r--zola/content/articles/_index.md3
-rw-r--r--zola/content/articles/safer-pointers.md97
-rw-r--r--zola/content/blog/_index.md3
-rw-r--r--zola/static/style.css9
-rw-r--r--zola/templates/base.html2
-rw-r--r--zola/templates/section.html14
-rw-r--r--zola/templates/taxonomy_list.html15
-rw-r--r--zola/templates/taxonomy_single.html28
10 files changed, 175 insertions, 6 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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 @@
<menu>
<li><a href="/">Home</a></li>
<li><a href="/articles">Articles</a></li>
+ <li><a href="/blog">Blog</a></li>
<li><a href="https://git.ndato.com/">Git</a></li>
<li><a href="/cv-dato.pdf">CV / Resume</a></li>
- <li><a href="/contact">Contact</a></li>
</menu>
</nav>
<header>Links</header>
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 %} &gt; {% endif %}<a href="{{ s.path | safe }}">{{ s.title }}</a>
{% endfor %}
&gt; <a href="{{ section.path | safe }}">{{ section.title }}</a>
@@ -16,7 +16,17 @@
<p><a href="{{ page.path | safe}}">{{ page.title }}</a></p>
</dt>
<dd>
- <p>{{ page.description }} <em><date datetime="{{ page.updated }}">{{ page.updated }}</date></em></p>
+ <p><em><date datetime="{{ page.date }}">{{ page.date }}</date></em>: {{ page.description }}</p>
+ <p>
+ {% for kind, values in page.taxonomies %}
+ {% set cat = get_taxonomy(kind=kind) -%}
+ <a href="{{ cat.permalink | safe }}">{{ kind }}</a>:
+ {% for v in values %}
+ {% if loop.index != 1%}, {% endif %}<a href="{{ get_taxonomy_url(kind=kind, name=v) | safe }}">{{ v }}</a>
+ {% endfor %}
+ <br/>
+ {% endfor %}
+ </p>
</dd>
{% endfor %}
</dl>
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 %}<a href="/">Home</a> &gt; <a href="{{ current_path | safe }}">{{ taxonomy.name }}</a>{% endblock %}
+{% block content %}
+<dl title="List of {{ taxonomy.name }} entries">
+{% for term in terms %}
+<dt>
+ <p><a href="{{ term.path | safe}}">{{ term.name }}</a></p>
+</dt>
+{% endfor %}
+</dl>
+{% 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 %}<a href="/">Home</a> &gt; {{ taxonomy.name }} &gt; <a href="{{ current_path | safe }}">{{ term.name }}</a>{% endblock %}
+{% block content %}
+<dl title="List of {{ term.name }} entries">
+{% for page in term.pages %}
+<dt>
+ <p><a href="{{ page.path | safe}}">{{ page.title }}</a></p>
+</dt>
+<dd>
+ <p><em><date datetime="{{ page.date }}">{{ page.date }}</date></em>: {{ page.description }}</p>
+ <p>
+ {% for kind, values in page.taxonomies %}
+ {% set cat = get_taxonomy(kind=kind) -%}
+ <a href="{{ cat.permalink | safe }}">{{ kind }}</a>:
+ {% for v in values %}
+ {% if loop.index != 1%}, {% endif %}<a href="{{ get_taxonomy_url(kind=kind, name=v) | safe }}">{{ v }}</a>
+ {% endfor %}
+ <br/>
+ {% endfor %}
+ </p>
+</dd>
+{% endfor %}
+</dl>
+{% endblock %}
+