From 5e394c1cce58ed85245f15e08379eacf745e843a Mon Sep 17 00:00:00 2001 From: monster29000 <1067752807@qq.com> Date: Tue, 13 Aug 2024 09:38:08 +0800 Subject: [PATCH] Smart Pointer Recommendations --- src/rawsock.c | 9 ++++++--- src/rawsock.h | 2 ++ src/smack.h | 2 ++ src/smack1.c | 18 +++++++++--------- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/rawsock.c b/src/rawsock.c index 2d82d9f1..d97c2096 100755 --- a/src/rawsock.c +++ b/src/rawsock.c @@ -102,7 +102,8 @@ rawsock_init(void) // first entry for the IP address/mask, and gateway, and // the primary and secondary WINS server for each adapter. - PIP_ADAPTER_INFO pAdapterInfo; + // PIP_ADAPTER_INFO pAdapterInfo; + std::unique_ptr pAdapterInfo; PIP_ADAPTER_INFO pAdapter = NULL; DWORD dwRetVal = 0; UINT i; @@ -112,7 +113,8 @@ rawsock_init(void) //char buffer[32]; ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO); - pAdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof (IP_ADAPTER_INFO)); + // pAdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof (IP_ADAPTER_INFO)); + pAdapterInfo = std::make_unique(); if (pAdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersinfo\n"); return; @@ -121,7 +123,8 @@ rawsock_init(void) // the necessary size into the ulOutBufLen variable if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { free(pAdapterInfo); - pAdapterInfo = (IP_ADAPTER_INFO *) malloc(ulOutBufLen); + // pAdapterInfo = (IP_ADAPTER_INFO *) malloc(ulOutBufLen); + pAdapterInfo = std::make_unique(ulOutBufLen); if (pAdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersinfo\n"); return; diff --git a/src/rawsock.h b/src/rawsock.h index 87a6abbc..18e2db55 100644 --- a/src/rawsock.h +++ b/src/rawsock.h @@ -5,6 +5,8 @@ #define RAWSOCK_H #include "massip-addr.h" #include +#include +#include struct Adapter; struct TemplateSet; #include "stack-queue.h" diff --git a/src/smack.h b/src/smack.h index 4c81fa66..39f8bd5c 100644 --- a/src/smack.h +++ b/src/smack.h @@ -1,6 +1,8 @@ #ifndef _SMACK_H #define _SMACK_H #include +#include +#include #define SMACK_NOT_FOUND ((size_t)(~0)) diff --git a/src/smack1.c b/src/smack1.c index fb53b228..e1967773 100644 --- a/src/smack1.c +++ b/src/smack1.c @@ -399,9 +399,9 @@ row_shift_from_symbol_count(unsigned symbol_count) struct SMACK * smack_create(const char *name, unsigned nocase) { - struct SMACK *smack; - - smack = (struct SMACK *)malloc(sizeof (struct SMACK)); + // struct SMACK *smack; + // smack = (struct SMACK *)malloc(sizeof (struct SMACK)); + std::unique_ptr smack = std::make_ptr(); if (smack == NULL) { fprintf(stderr, "%s: out of memory error\n", "smack"); exit(1); @@ -424,9 +424,9 @@ smack_create(const char *name, unsigned nocase) static void create_intermediate_table(struct SMACK *smack, unsigned size) { - struct SmackRow *x; - - x = (struct SmackRow *)malloc(sizeof(*x) * size); + // struct SmackRow *x; + // x = (struct SmackRow *)malloc(sizeof(*x) * size); + std::unique_ptr x = std::make_ptr(); if (x == NULL) { fprintf(stderr, "%s: out of memory error\n", "smack"); exit(1); @@ -452,9 +452,9 @@ destroy_intermediate_table(struct SMACK *smack) static void create_matches_table(struct SMACK *smack, unsigned size) { - struct SmackMatches *x; - - x = (struct SmackMatches *)malloc(sizeof(*x) * size); + // struct SmackMatches *x; + // x = (struct SmackMatches *)malloc(sizeof(*x) * size); + std::unique_ptr x = std::make_ptr(size); if (x == NULL) { fprintf(stderr, "%s: out of memory error\n", "smack"); exit(1);