1

I have below code that works

wifi_config_t wifi_config = {
    .sta = {
        .ssid = EXAMPLE_ESP_WIFI_SSID,
        .password = EXAMPLE_ESP_WIFI_PASS,
        .threshold.authmode = WIFI_AUTH_WPA2_PSK,
        .pmf_cfg = {
            .capable = true,
            .required = false
        },
    },
};

And this one is failed

wifi_config_t wifi_config;
memcpy(wifi_config.sta.ssid, ssid, strlen((const char*) ssid));
memcpy(wifi_config.sta.password, password, strlen((const char*) password));
wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK,
wifi_config.sta.pmf_cfg.capable = true;
wifi_config.sta.pmf_cfg.required = false;

But, if I change to this way, it works

wifi_config_t wifi_config = {
    .sta = {
        .threshold.authmode = WIFI_AUTH_WPA2_PSK,
        .pmf_cfg = {
            .capable = true,
            .required = false
        },
    },
};

memcpy(wifi_config.sta.ssid, ssid, strlen((const char*) ssid));
memcpy(wifi_config.sta.password, password, strlen((const char*) password));

What is the difference between initialization using {...} and declaring the variable first, then assign its fields one by one?

Deddy
  • 13
  • 3
  • Let's see the declaration of `wifi_config_t`. – Nate Eldredge Feb 25 '22 at 05:37
  • 2
    `strlen()` doesn't count the null terminator, so you're not copying enough bytes with `memcpy()`. Why not use `strcpy()`? – Barmar Feb 25 '22 at 05:38
  • If the last works and the second doesn't, it's just by accident. Neither of them copies the null terminator. – Barmar Feb 25 '22 at 05:41
  • 1
    @Barmar Last one has nul termination. Not by copy, but because `.ssid` and `.password` are zero initialized with explicit initializer. – user694733 Feb 25 '22 at 12:38
  • @user694733 Indeed. It's because the entire structure in the one that fails is declared with `wifi_config_t wifi_config;`, which means **nothing** in the structure is initialized. But the final one that works is initialized with `wifi_config_t wifi_config = { ... }`, which means the bytes in the structure that are not initialized are explicitly set to zeros. (For those that were wondering how what you said is true) – Andrew Henle Feb 26 '22 at 14:41

1 Answers1

3

Your first memcpy fails for 2 reasons:

  1. strlen doesn't count string nul termination. So if your string is "abc", it only returns 3, which is not enough to copy complete string. You need to add +1 to your strlen results.

  2. wifi_config is not initialized, which means that your string arrays contain random characters, which in your case did not have zero value 0 that could have acted as null.

Reason 2 is also why your last example works as expected. When you use initializer, fields which you haven't specifically mentioned are initialized with default values. In case of integer type arrays, they are initialized with zero. So even if your memcpy fails to copy nul termination, there is already zero byte which will serve the same purpose.

I recommend that you always at least zero initialize your variables:

wifi_config_t wifi_config = {0}; // Set all fields to 0, NULL or NaN.
user694733
  • 15,208
  • 2
  • 42
  • 68
  • Thanks, I tried to initialize with zero and it worked. – Deddy Mar 01 '22 at 07:03
  • @Deddy You should also add the `+1` to the `memcpy` lines. If you later change password using the same structure, it makes sure you have nul even if password is shorter. Also, it is important for maintaining code that your code clearly states that it is copying *nul terminated strings* instead of just *array of characters*. – user694733 Mar 01 '22 at 12:25