0

Here's my code:

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)
#pragma comment(lib,"ws2_32.lib")
#include "stdafx.h"
#include <assert.h>
#include "Bootpd.h"
#include <iostream>
#include <string>
#include <iphlpapi.h>


char *MAC() {
    PIP_ADAPTER_INFO AdapterInfo;
    DWORD dwBufLen = sizeof(AdapterInfo);
    char *mac_addr = (char*)malloc(20);

    AdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
    assert(AdapterInfo != NULL); //Error allocating memory

    // Make an initial call to GetAdaptersInfo to get the necessary size into the dwBufLen variable
    if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == ERROR_BUFFER_OVERFLOW) {
        AdapterInfo = (IP_ADAPTER_INFO *)malloc(dwBufLen);
        assert(AdapterInfo != NULL);
    }

    if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == NO_ERROR) {
        PIP_ADAPTER_INFO info = AdapterInfo; //Copy information
        sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X",
            info->Address[0], info->Address[1],
            info->Address[2], info->Address[3],
            info->Address[4], info->Address[5]);
    }
    free(AdapterInfo);
    return mac_addr;
}

I am using VS 2015 on Windows 10. I am trying to format my network adapter's MAC address information to look like a MAC address (aa:bb:cc:dd:ee:ff). I already tried defining _CRT_SECURE_NO_WARNINGS and disabling warning 4996 above my #include statements with no success. Is there anything I am missing, or does anybody know a different work around to get rid of the sprintf error variable may be unsafe? Thanks.

CKE
  • 1,533
  • 19
  • 18
  • 29
  • 2
    Turning off warnings is probably the wrong path to take. What's causing the warnings? Is this a type mismatch on `%02X` vs. whatever `Address[0]` is? – tadman Jul 06 '18 at 19:49
  • 1
    _"C4996 occurs when the compiler encounters a function or variable that is marked as deprecated. "_ source: https://msdn.microsoft.com/en-us/library/ttcz0bys.aspx As you don't include the full message text it's going to be difficult to help you further. – Richard Critten Jul 06 '18 at 19:54
  • 2
    Since this is C++, why not use `std::string` and friends instead of all this ugly manual allocation using `malloc`? There's ways of [combining multiple strings together with formatting](https://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf). – tadman Jul 06 '18 at 19:55
  • On a side note, you are initializing `dwBufLen` incorrectly. You are setting it to `sizeof(AdapterInfo)`, but `AdapterInfo` is a pointer, so `dwBufLen` will be 4 or 8, depending on 32bit or 64bit compiling. But then you allocate `sizeof(IP_ADAPTER_INFO)` bytes with `malloc()` and ignore `dwBufLen`. So your 1st call to `GetAdaptersInfo()` is lying about the true buffer size, passing in a buffer size that is much smaller than the actual buffer. Initialize `dwBufLen` as either `sizeof(*AdapterInfo)` or `sizeof(IP_ADAPTER_INFO)` instead, and then pass `dwBufLen` to the 1st call to `malloc()`. – Remy Lebeau Jul 06 '18 at 23:18
  • `assert.h` is a deprecated C-compatibility header in C++, use `cassert` and friends instead. You also don't include the headers necessary for `sprintf` and `malloc`/`free` – `cstdio` and `cstdlib`. You'll also have to use the functions for the C++ header equivalents from the `std` namespace (e.g. `std::sprintf`, `std::malloc`, `std::free`) to be standard compliant, since implementations aren't required to put the function definitions into the global namespace. When programming in C++, you should always use `nullptr` instead of `NULL` for better type safety. – tambre Jul 07 '18 at 05:09
  • Thanks for the fixes everyone. Using the std namespace seems to be working. I will some of my other mistakes now. – Justin Walker Jul 09 '18 at 12:33

1 Answers1

1

The compiler is complaining about the possibility of overrunning the mac_addr array. Give sprintf_s a shot. https://en.cppreference.com/w/c/io/fprintf