diff --git a/coreapi/lpconfig.c b/coreapi/lpconfig.c index 9dcce9f4d..f96966eac 100644 --- a/coreapi/lpconfig.c +++ b/coreapi/lpconfig.c @@ -271,7 +271,7 @@ static LpSection* lp_config_parse_line(LpConfig* lpconfig, const char* line, LpS /* remove ending white spaces */ for (; pos2>pos1 && pos2[-1]==' ';pos2--) pos2[-1]='\0'; - if (pos2-pos1>=0){ + if (pos2-pos1>0){ /* found a pair key,value */ if (cur!=NULL){ @@ -457,10 +457,10 @@ int lp_config_get_int(const LpConfig *lpconfig,const char *section, const char * const char *str=lp_config_get_string(lpconfig,section,key,NULL); if (str!=NULL) { int ret=0; - + if (strstr(str,"0x")==str){ sscanf(str,"%x",&ret); - }else + }else sscanf(str,"%i",&ret); return ret; } @@ -493,7 +493,7 @@ void lp_config_set_string(LpConfig *lpconfig,const char *section, const char *ke if (sec!=NULL){ item=lp_section_find_item(sec,key); if (item!=NULL){ - if (value!=NULL) + if (value!=NULL && value[0] != '\0') lp_item_set_value(item,value); else lp_section_remove_item(sec,item); }else{ @@ -542,12 +542,19 @@ void lp_config_set_float(LpConfig *lpconfig,const char *section, const char *key void lp_item_write(LpItem *item, FILE *file){ if (item->is_comment) fprintf(file,"%s",item->value); - else + else if (item->value && item->value[0] != '\0' ) fprintf(file,"%s=%s\n",item->key,item->value); + else { + ms_warning("Not writing item %s to file, it is empty", item->key); + } } void lp_section_param_write(LpSectionParam *param, FILE *file){ - fprintf(file, " %s=%s", param->key, param->value); + if( param->value && param->value[0] != '\0') { + fprintf(file, " %s=%s", param->key, param->value); + } else { + ms_warning("Not writing param %s to file, it is empty", param->key); + } } void lp_section_write(LpSection *sec, FILE *file){ diff --git a/tester/rcfiles/zero_length_params_rc b/tester/rcfiles/zero_length_params_rc new file mode 100644 index 000000000..108749a5d --- /dev/null +++ b/tester/rcfiles/zero_length_params_rc @@ -0,0 +1,3 @@ +[test] +zero_len= +non_zero_len=test diff --git a/tester/setup_tester.c b/tester/setup_tester.c index aa1a6f73e..6d0631235 100644 --- a/tester/setup_tester.c +++ b/tester/setup_tester.c @@ -108,8 +108,41 @@ static void linphone_lpconfig_from_buffer(){ conf = lp_config_new_from_buffer(buffer_linebreaks); CU_ASSERT_STRING_EQUAL(lp_config_get_string(conf,"buffer_linebreaks","test",""),"ok"); lp_config_destroy(conf); - } + +static void linphone_lpconfig_from_buffer_zerolen_value(){ + /* parameters that have no value should return NULL, not "". */ + static const char* zerolen = "[test]\nzero_len=\nnon_zero_len=test"; + LpConfig* conf; + + conf = lp_config_new_from_buffer(zerolen); + + CU_ASSERT_STRING_EQUAL(lp_config_get_string(conf,"test","zero_len","LOL"),"LOL"); + CU_ASSERT_STRING_EQUAL(lp_config_get_string(conf,"test","non_zero_len",""),"test"); + + lp_config_set_string(conf, "test", "non_zero_len", ""); /* should remove "non_zero_len" */ + CU_ASSERT_STRING_EQUAL(lp_config_get_string(conf,"test","non_zero_len","LOL"), "LOL"); + + lp_config_destroy(conf); +} + +static void linphone_lpconfig_from_file_zerolen_value(){ + /* parameters that have no value should return NULL, not "". */ + static const char* zero_rc_file = "zero_length_params_rc"; + char* rc_path = ms_strdup_printf("%s/rcfiles/%s", liblinphone_tester_file_prefix, zero_rc_file); + LpConfig* conf; + + conf = lp_config_new(rc_path); + + CU_ASSERT_STRING_EQUAL(lp_config_get_string(conf,"test","zero_len","LOL"),"LOL"); + CU_ASSERT_STRING_EQUAL(lp_config_get_string(conf,"test","non_zero_len",""),"test"); + + lp_config_set_string(conf, "test", "non_zero_len", ""); /* should remove "non_zero_len" */ + CU_ASSERT_STRING_EQUAL(lp_config_get_string(conf,"test","non_zero_len","LOL"), "LOL"); + + lp_config_destroy(conf); +} + void linphone_proxy_config_address_equal_test() { LinphoneAddress *a = linphone_address_new("sip:toto@titi"); LinphoneAddress *b = linphone_address_new("sips:toto@titi"); @@ -191,6 +224,8 @@ test_t setup_tests[] = { { "Linphone random transport port",core_sip_transport_test}, { "Linphone interpret url", linphone_interpret_url_test }, { "LPConfig from buffer", linphone_lpconfig_from_buffer }, + { "LPConfig zero_len value from buffer", linphone_lpconfig_from_buffer_zerolen_value }, + { "LPConfig zero_len value from file", linphone_lpconfig_from_file_zerolen_value }, { "Chat room", chat_root_test } };