Implicit Type Conversions Gone Wrong, C++ Edition

Consider the following setup:

#include <cstddef>
using namespace std;
// ...
size_t n = 100;
size_t* array = new size_t[n];

The goal is to set every element of the array to the fixed value x.

In C++, an idiomatic way to solve this problem are the functions std::fill (link to documentation) and std::fill_n (link to documentation) from the STL header algorithm. In addition to the fixed value x, std::fill requires a pair of output iterators specifying a half-open range as its arguments whereas the additional argument to std::fill_n is an output iterator pointing to the first element and the length of the container. That is, we could assign to the elements of the array above as follows:

#include <cstddef>
#include <algorithm>
using namespace std;
// ...
size_t n = 100;
size_t* array = new size_t[n];

fill(array, array+n, x);
fill_n(array, n, x);

What made me write this blog post is the following line of code:

fill_n(array, array+n, x);

Here, I accidentally used std::fill_n in place of std::fill but the code still compiles because of the automatic, implicit type conversion (type coercion) from std::size_t* to std::size_t. With warnings enabled (-Wextra -Wall -std=c++11 -pedantic) neither g++ 4.8.5 nor clang++ 3.5 warn about this line and yet this piece of code causes a segmentation fault on my computer whenever it is executed.

More Secure Wireless Telephony with VoIP over Wi-Fi

In this post, I will explain why you should start to phase out DECT and how you can do this with VoIP (Voice over IP, also known as Internet telephony) over Wi-Fi. This guide allows you to attach several wireless devices to your landline and it permits calls among all telephones in your household. Moreover, the guide provides encrypted wireless communication, it uses off-the-shelf consumer products, and works worldwide. On the downside, the voice quality may be worse unless your router supports WMM (Wireless Multimedia Extensions) and you may need a WLAN repeater because DECT tends to have higher range than WLAN. (Surprisingly, the electronics markets in my vicinity offer dual-band WLAN repeaters with 600 MBit/s transfer rate fitting into a wall socket costing no more than the cheapest DECT repeater.) If so desired, you can continue to use your existing DECT phones but communication with these devices is not guaranteed to be encrypted.

You can follow this guide if you have

  • an account with an Internet telephony provider,
  • your Internet router is a Wi-Fi hotspot,
  • the Internet router manages your landline,
  • the Internet router allows wireless VoIP telephony using SIP, and
  • a mobile device with Wi-Fi as well Android, iOS, or Windows.

Continue reading More Secure Wireless Telephony with VoIP over Wi-Fi

Matlab: Unawareness of Partial Path Names may cause Bugs

In a filesystem, there are two kinds of paths: relative paths and absolute paths. Absolute paths always point to the same target while the target of a relative path depends on the current working directory. In Matlab, paths are called "path names" and there is a third kind of path name: the partial path name. It is defined as follows:

A partial path name is the last portion of a full path name for a location on the MATLAB search path.

(The Matlab search path is similar to the environment variable PATH and is used to locate Matlab files.) In consequence of this definition, a path name may be treated by Matlab as a partial path name even if the user is unaware of them.

In this post, I will demonstrate Matlab behavior that is unexpected if you are not aware of the existence of partial path names. I will also explain how this causes a bug in xUnit 3 and xUnit 4.

Continue reading Matlab: Unawareness of Partial Path Names may cause Bugs