Browse Source

Small improvement to readability of freeing

Piotr Czajkowski 6 years ago
parent
commit
9a6200279b
2 changed files with 6 additions and 5 deletions
  1. 4 4
      comments.c
  2. 2 1
      keyval.c

+ 4 - 4
comments.c

@@ -12,7 +12,8 @@ char* anonymizeAuthor(dictionary *authors, xmlChar const *authorName) {
 
 	asprintf(&newName, "Author%d", authors->length+1);
 	dictionary_add(authors, name, newName);
-	return newName;
+	free(newName);
+	return (char*)dictionary_find(authors, name);
 }
 
 void printAuthors(dictionary *authors) {
@@ -23,14 +24,13 @@ void printAuthors(dictionary *authors) {
 int processAuthors(xmlXPathObjectPtr authors) {
 	dictionary *anonAuthors = dictionary_new();
 	
-	xmlChar *authorName = (xmlChar*)"";
 	for (int i=0; i < authors->nodesetval->nodeNr; i++){
-		
+		xmlChar *authorName = (xmlChar*)"";		
 		authorName = xmlNodeGetContent(authors->nodesetval->nodeTab[i]);
 		char *anonAuthor = anonymizeAuthor(anonAuthors, authorName);
 		xmlNodeSetContent(authors->nodesetval->nodeTab[i], (xmlChar*)anonAuthor);
+		xmlFree(authorName);
 	}
-	xmlFree(authorName);
 
 	printAuthors(anonAuthors);
 	dictionary_free(anonAuthors);

+ 2 - 1
keyval.c

@@ -1,11 +1,12 @@
 // Borrowed from https://github.com/b-k/21st-Century-Examples
 #include <stdlib.h> //malloc
+#include <string.h>
 #include <strings.h> //strcasecmp (from POSIX)
 #include "keyval.h"
 
 keyval *keyval_new(char *key, void *value){
     keyval *out = malloc(sizeof(keyval));
-    *out = (keyval){.key = key, .value=value};
+    *out = (keyval){.key = strdup(key), .value=strdup(value)};
     return out;
 }